date_default_timezone_set() seems to have no effect - php

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.

Related

PHP date() not returning correct time?

I'm trying to figure out why my current PHP date() function is not returning the correct time.
As of this moment, my real-life date/time is 2015:10:23 18:49(sec), however for some reason when I run my php code, I get 2015-10-24 00:49:15. For some reason it thinks I'm 6hrs ahead? I'm in Eastern Standard Time by the way, in case it helps.
PHP
$today = date("Y-m-d H:i:s");
echo "The date and time right now is " . $today;
I'm attempting to code the current date/time into my MySQL database, to log when a member created their account (and later update to log when why last logged in). How do I fix this so it's correct for anyone who uses it?
include this line on the top of your php code.
date_default_timezone_set('Asia/Beirut');
while instead of asia/beirut, get your country..
check the link to see all supported countries
http://php.net/manual/en/timezones.php

gmmktime(): You should be using the time() function instead

What is the reason of the following error? How to solve the issue?
gmmktime(): You should be using the time() function instead
The issue in the line number 90:
89 date_default_timezone_set("GMT");
90 $time = gmmktime();
gmmktime() internally uses mktime() , which throws an E_STRICT notice when called with no arguments, so use the time() function instead.
This is a comment from php.net
gmmktime() should ONLY be used to create a timestamp when specifying a
specific GMT date and time.
If you want to make a valid time stamp for the current date & time,
use mktime() instead.
UNIX timestamps, by definition, store the GMT time relative to the
UNIX epoch.
gmmktime() (without any parameters specified) will effectively use the
computer's LOCAL time values just the same as if they were explicit
parameters, and the resulting time stamp will be incorrect. (The
resulting timestamp will actually be offset in the OPPOSITE direction
of the local timezone offset from GMT!)
So it requires input. You should consider this function as a conversion mechanism rather than a source of formatted data.
I suspect that your host may have updated php.
If you ask your host provider to edit your php.ini file, to disable logging of STRICT warnings in, php.ini, you may be sorted.
Also according to php.net
gmmktime() internally uses mktime() so only times valid in derived local time can be used.
Also check the change log there, it says
As of PHP 5.1.0, the is_dst parameter became deprecated. As a result, the new timezone handling features should be used instead.
The issue is that gmmktime() and mktime() require input, i.e. they take year, month, day, etc and give a timestamp. If you just want the current timestamp use the time() function.
This works fine for this purpose: gmdate( 'U' )

strtotime returns different results on different PHP versions

I came across a weird problem with strtotime function returning different (and I think incorrect) results.
This is my test code and results:
$fromTime = strtotime('2013-10-22');
$toTime = strtotime('2013-10-28');
$days = ($toTime - $fromTime) / (3600 * 24);
var_dump($days, $fromTime, $toTime);
// PHP 5.2.5 on codepad.org - correct
// int(6)
// int(1382400000)
// int(1382918400)
// PHP 5.3.15, PHP 5.4.6 - incorrect I guess
// float(6.0416666666667)
// int(1382392800)
// int(1382914800)
You can see it live on http://codepad.org/gdTqFLqG.
Do you have any idea why is this happening?
I agree with #N.B.'s suggestion to use the DateTime class instead -- You shouldn't really be using strtotime() for this kind of thing today.
However, as for why it's happening, look at the dates you're comparing.... what often happens between those dates? Yep, that's right -- daylight savings.
So my guess is that it's got nothing to do with the PHP version, and more to do with the timezone setup on the different platforms you're testing. One is set to use the UCT and the other is set to use a local timezone that has DST.
You should use date_default_timezone_set(), cause in second test you got correct time with some UTC+- correction.

Php Date Function

I have tried using php date function() like as follows
$date=date('Y-m-d').' '.date('H:i:s');
echo $date;
the output displayed is 2013-04-03 09:04:02.. but my system is 02:49 pm...
What time is being displayed for me? I tried changing the internet timing even then I am getting the same answer ?
First off, it is not necessary to use the date function twice. This will do the same thing:
echo date('Y-m-d H:i:s');
Second, you need to set PHP's date.timezone. This can be done in the php.ini file, but it can also be done using the date_default_timezone_set function, like this:
date_default_timezone_set('Europe/Amsterdam');
The string that you have to put in can be found in the documentation.
It may also be worth noting that you can tell the date function to use any time. This is done by passing in a *nix timestamp as the second argument. For example:
// One week ago from now
echo date('Y-m-d H:i:s', time()-604800);
It will show server's time only. If possible compare with your server time. If you want to use local machine's time you need to go with JAVASCRIPT.
And another suggestion,
You don't have to use individually to display date & time. You can achieve this in a single statement like this.
$date=date('Y-m-d H:i:s');
You will get the same format 2013-04-03 09:04:02
check for your system timezone and your default timezone in php by opening phpinfo()

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

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.

Categories