Reputation: 6344
See the below code
$compDate = date('d/m/y',strtotime('-2 weeks'));
echo strtotime($compDate)."-->".strtotime('-2 weeks');
The echo outputs 1398882600-->1388938336
.
Why does the time stamp differ?
Upvotes: 2
Views: 761
Reputation: 37075
strtotime
assumes x/x/x
notation is American and x-x-x
notation as European. Since you are passing in d/m/Y
as the formatting for $compDate
and then passing that date string through strtotime()
as second time, it is interpreting the date string as m/d/Y
the second time. You can confirm by changing your code like this:
$compDate = strtotime('-2 weeks');
echo $compDate . "-->" . strtotime('-2 weeks');
or this way:
$compDate = date('d-m-y',strtotime('-2 weeks'));
echo strtotime($compDate) . "-->" . strtotime('-2 weeks');
or this way:
$compDate = date('m/d/y',strtotime('-2 weeks'));
echo strtotime($compDate) . "-->" . strtotime('-2 weeks');
A better solution would be to use the DateTime
class, which allows you to define the date format of a given string using createFromFormat
so that the parser knows which number is which:
$compDate = date('d/m/y',strtotime('-2 weeks'));
$compDateObject = DateTime::createFromFormat('d/m/y', $compDate);
echo $compDateObject->format('U') . "-->" . strtotime('-2 weeks');
That last example is a bit convoluted because generally with a relative string like -2 weeks
you wouldn't also need to worry about the formatting, but I'm guessing the issue you're really having is that the date format used throughout your code is in d/m/y
, so the above gives an idea of how to swap between that format and and epoch timestamp. If you want to use DateTime
to get a relative date (like -2 weeks
), you could revamp the above as:
$compDate = new DateTime('-2 weeks');
echo $compDate->format('U') . "-->" . strtotime('-2 weeks');
echo $compDate->format('d/m/Y');
If you want the timestamp to be for the date but don't want to use time, you can add today
to the relative format, like so:
$compDate = new DateTime('2 weeks ago today');
echo $compDate->format('U') . "-->" . strtotime('-2 weeks 00:00');
echo $compDate->format('d/m/Y');
Upvotes: 0
Reputation: 10638
The first problem is the wrong format - as stated by Sharanya Dutta. The second one is you are losing precision when formatting to m/d/y
instead of m/d/y, H:i:s
eg.
$compDate = date('m/d/y',strtotime('-2 weeks'));
print date("d.m.Y @ H:i:s", strtotime($compDate)); //05.01.2014 @ 00:00:00
print date("d.m.Y @ H:i:s", strtotime('-2 weeks')); //05.01.2014 @ 17:23:42
When you add hours, minutes and seconds to $compDate
, it will work as expected
$compDate = date('m/d/y, H:i:s',strtotime('-2 weeks'));
var_dump( strtotime($compDate) === strtotime('-2 weeks')); // TRUE
If you don't need the time but only the date, you can set the time in the strtotime()
as well.
$compDate = date('m/d/y',strtotime('-2 weeks'));
var_dump( strtotime($compDate) === strtotime('-2 weeks, 0:0:0')); // TRUE
Upvotes: 0
Reputation:
This is from the PHP manual:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.
So change d/m/y
to either d-m-y
or m/d/y
and strtotime will work perfectly.
Update: Yes, kingkero is right. You have to change d/m/y
to either d-m-y H:i:s
or m/d/y H:i:s
. The point is that you can’t ignore the hour, minute and second.
Upvotes: 2