473,394 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Subtracting day from date

384 256MB
I have a script that outputs 'Today' or 'Yesterday' for the date a piece of content was posted but if its the first of the month yesterday don't work.

Is there a way to subtract one day from the date variable, the below code I'm using at the moment is:
Expand|Select|Wrap|Line Numbers
  1. if(date("dmY") == date("dmY", strtotime($datetimeIn)))
  2.     $datetime = 'Today @ '.date("H:i", strtotime($datetimeIn));
  3. else if((date("d")-1).date("mY") == date("dmY", strtotime($datetimeIn)))
  4.     $datetime = 'Yesterday @ '.date("H:i", strtotime($datetimeIn));
  5. else
  6.     $datetime = date("D jS, F Y @ H:i", strtotime($datetimeIn));
Oct 1 '10 #1

✓ answered by code green

Expand|Select|Wrap|Line Numbers
  1. else if(date("dmY")-86400 
You cannot subtract 86400 off the return from date() because date() returns a string
Expand|Select|Wrap|Line Numbers
  1. else if(strtotime(date("dmY"))-86400 == date("dmY", $date)) 
And here you are comparing a string to timestamp which is an integer.
If you tried doing that in C++ you would be arrested.
Try
Expand|Select|Wrap|Line Numbers
  1. if(date('Ymd',strtotime('yesterday')) == date("dmY", $date)))
Or
Expand|Select|Wrap|Line Numbers
  1. if(date('Ymd',time()-86400)) == date("dmY", $date)))
Might need a little tweaking

6 3119
code green
1,726 Expert 1GB
Is there a way to subtract one day from the date variable
Well you don't have a $date variable,
but as you are using unix timestamp this is -86400
Oct 1 '10 #2
ziycon
384 256MB
If I did it like this would it make it easier to subtract a date?
Expand|Select|Wrap|Line Numbers
  1. $date = strtotime($datetimeIn);
  2.  
  3. if(date("dmY") == date("dmY", $date))
  4.      $datetime = 'Today @ '.date("H:i", $date);
  5. else if((date("d")-1).date("mY") == date("dmY", $date))
  6.      $datetime = 'Yesterday @ '.date("H:i", $date);
  7. else
  8.      $datetime = date("D jS, F Y @ H:i", $date);
Oct 1 '10 #3
code green
1,726 Expert 1GB
In your code $datetime is a string.
$date is a timestamp.
$date - 86400 is one day less
But if you are comparing dates using strings, strtotime is very clever, it even understands things like 'yesterday' and '-1 day'.
Oct 1 '10 #4
ziycon
384 256MB
I get you now, I'm just a bit confused as to how I'm going wrong implementing it.

I want the current date to be subtracted by one day, so I'm using the below code but I know I'm doing it wrong but not sure what I'm missing?
Expand|Select|Wrap|Line Numbers
  1. else if(date("dmY")-86400 == date("dmY", $date))
or should it be:
Expand|Select|Wrap|Line Numbers
  1. else if(strtotime(date("dmY"))-86400 == date("dmY", $date))
Oct 1 '10 #5
code green
1,726 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. else if(date("dmY")-86400 
You cannot subtract 86400 off the return from date() because date() returns a string
Expand|Select|Wrap|Line Numbers
  1. else if(strtotime(date("dmY"))-86400 == date("dmY", $date)) 
And here you are comparing a string to timestamp which is an integer.
If you tried doing that in C++ you would be arrested.
Try
Expand|Select|Wrap|Line Numbers
  1. if(date('Ymd',strtotime('yesterday')) == date("dmY", $date)))
Or
Expand|Select|Wrap|Line Numbers
  1. if(date('Ymd',time()-86400)) == date("dmY", $date)))
Might need a little tweaking
Oct 1 '10 #6
ziycon
384 256MB
Appreciate your help, the two solution you gave worked, I went with the second one. Thanks again for your help.
Oct 1 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: dlx_son | last post by:
Here is the code so far <form name="thisform"> <h3>Enter time to add to or subtract from:</h3> (If not entered, current time will be used)<br> Day: <input name="d1" alt="Day of month"...
11
by: Laery | last post by:
Hi, I'm currently adding a new module to an old borland C3.1 application (dos). And I need to calculate a date by subtracting the number of days from a given date. I know I could use an...
0
by: tohear | last post by:
ToOADate and FromOADate don't handle negative dates with time parts correctly. Negative dates with no time parts are OK. (Note: In COM date "zero" is 12/30/1899). Does anyone know if these are...
8
by: Ifollowhim | last post by:
I have a (installation) date in a table that I put in manually. It is the date we want to complete a job. In a form that uses data from that table I want to add a text box that is bound to the...
26
by: jshanman | last post by:
I am writing a timeline that uses Google Maps. I have a function that converts a date time to latitude coords. This function is used to draw the markers on the timeline. I need a reverse function...
31
by: Spiro Trikaliotis | last post by:
Hello, I have a question regarding subtracting a pointer from another one. Assume I have two pointers p1 and p2, which both point to a memory area obtained with malloc(). Assume p1 = p2 + some...
5
by: dan | last post by:
I could find no documentation that the Date object has useful wrapping properties. For example, write(new Date(2007, 11 + 1, 1)) results in January 1st 2008. And write(new Date(2007, 0, 31 + 1))...
10
by: dan | last post by:
Am i breaking any rules when I loop dates like // Determine Memorial Day intFlag = 0; memDayHol = new Date (currentYear, 4, 31); while (intFlag == 0) { if (memDayHol.getDay() == 1) {intFlag...
0
by: manofariz | last post by:
I have a timeclock that produces a steady flow of scans into an access table. I have a query off that table that organizes the scans by person and by date. I also have a report that gives me the...
3
by: shmoopie | last post by:
Hi, I have a php form that I want to use to pass a user specified "start date" and "end date" to a mysql database to retrieve reservations. I want the number of days up to and including the "end...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.