Why is strtotime($lastmoment) bigger than time()? - php

The following code
echo $lastmoment."<br/>";
echo time();
echo "<br/>";
echo strtotime($lastmoment);
outputs:
2009-12-15 17:40:53 1260876829
1260898853
What's wrong? $lastment is a past time stamp in MySQL, why is strtotime($lastmoment) bigger than time()?

If your MySQL server is a different machine, or if the timestamp in $lastmoment was set from a different machine, you could be seeing clock drift. Check the system clocks on the various machines, and see if they agree.

Because the time() function was may be executed some milliseconds before the $lastmoment.
EDIT:
Adding the comment to my thoughts, it might me that, the convert of strtotime() might have failed.
Or the date value from the database contains a value e.g. days which the date() does not have.
Be sure, that you compare 2 datevalues of the same format.

Related

PHP date() returns different timezones for different timestamps

I've faced a very weird behaviour of php date() function.
See this code:
date_default_timezone_set('Australia/Melbourne');
echo date('P', 1475000000) . ' ' . date('P', 1475700000);
It returns +10:00 +11:00
While it must be always +10:00. Unix timestamps don't content timezone in it that's why date() must return just currently configured timezone.
PHP version 5.6.23
Any ideas why is that?
I found an answer.
It's because Australia changes time on 2nd October. One timestamp is before it, while another is after, that's why all correct.
Thanks all for attention.
http://www.australia.gov.au/about-australia/facts-and-figures/time-zones-and-daylight-saving
Your second timestamp is with DST. The first one is not yet.

Why time() function returns different values on different servers?

When I write this line of code on local, the output will be this:
echo time(); // 1464605083
And when I write in on the 3v4l (online PHP shell) the output will be this:
echo time(); // 1339412843
As you see there is a huge different between those outputs. What's wrong? And how can I sync them?
Note: I'm using Xampp on my local.
If the time you got back from the code is not the right time, it's probably because your server is in another country or set up for a different timezone.
So, if you need the time to be correct according to a specific location, you can set a timezone to use.
The example below sets the timezone to "America/New_York", then outputs the current time in the specified format:
<?php
date_default_timezone_set("America/New_York");
echo "The time is " . date("h:i:sa");
?>
That timezone will be vary according to the servers you are using. If you want to use same timezone in all your server. Use
date_default_timezone_set('timezonename')
use timezone i hope it's work ...
<?php
date_default_timezone_set('America/New_York');
echo $date= time() ;
?>
The webserver where you are testing this has a completely off system time set.
If you look at the date of your test script (right next to the title field) you'll see that it has been saved at # Mon Jun 11 2012 11:07:23.
Your timestamp 1339412843 translates to 06/11/2012 # 11:07am (UTC).
So, the server time is just wrong. Test your script someplace else if time is critical.

date_default_timezone_set() seems to have no effect

I was trying to get the time() on a PHP page (localhost), but it seemed it was returning wrong dates. So I tried:
date_default_timezone_set("America/Chicago");
echo "America/Chicago:".time();
echo "<br>";
date_default_timezone_set("Europe/Helsinki");
echo "Europe/Helsinki:".time();
Which outputs:
America/Chicago:1439981623
Europe/Helsinki:1439981623
How is it that these two values are the same? How can I make sure this particular php-page returns times in my timezone, without changing the php.ini file?
As stated at php.net documentation's first highly voted contribution note by "Timo K":
The function time() returns always timestamp that is timezone
independent (=UTC).
That said, you must use DateTime objects and it's methods to deal with multiple timezones.

gmdata in php not working properly

It's a simple question..but drive me 2 madness.
the result of this simple line of code:
echo gmdate('Y/m/d H:i:s');
... must output GMT time but it get it minus 1 hour!!!!!
So why??
Greenwich Mean Time has no "Summer Time" or "Daylight Saving Time" so depending on the season of the year these statements may produce the same or different output.
date_default_timezone_set('Europe/London');
echo gmdate('c');
echo date('c');
-- from the PHP manual (so, in addtion to the answer you get a clear RTM ;)
More information about timezones and daylight saving on SO.
Compare the output of your script with:
http://www.worldtimeserver.com/current_time_in_UTC.aspx
If it's wrong, the computer where you run the code is probably misconfigured.

cannot get the correct date and time in php

I dont know why I'm not getting the correct date and time in my region.
Here's the code
<?php
//date_default_timezone_set('Asia/Manila');
echo date('YYYY-mm-dd H:i:s'); ?>
Even if I comment out or change the time zone the date and time that I'm getting is still the same.
The date today is 23. But its outputting 22. And the time doesn't change even if I change the time zones.
Does it have something to do with my computer?
Because I sometimes notice that the clock on the lower right corner of the screen is not displaying the correct time.
Heres the current time, but its displaying this:
01-22-2011 05:38:31-PM
Are you sure that's the script producing the output shown?
echo date('YYYY-mm-dd H:i:s'); ?>
Doesn't seem to match the format of
01-22-2011 05:38:31-PM
Other than that, the timezone setting looks right.
It could be that time set up on server (where PHP script is being executed) is not correct.
Are you from the Philippines? This worked for me for some reason:
date_default_timezone_set('Asia/Tbilisi');
Asia/Manila is not working for me too. Just give it a try.

Categories