I'm using Vagrant box (Homestead) where the timezone is set to Europe/Brussels.
Current time that I get in terminal using command "date", returns this value:
Sun Mar 25 23:27:40 CEST 2018
In both cli & fpm php.ini file I've:
date.timezone = "Europe/Brussels"
Running following code simultaneously:
print_r(new DateTime());
print_r(time());
Gives these results:
For DateTime():
[
"date" => "2018-03-25 23:27:43.650908",
"timezone_type" => 3,
"timezone" => "Europe/Brussels"
]
And the epoch timestamp for time()
1522013263
Converting the latter gives:
Sun, 25 Mar 2018 21:27:43 GMT
Given the results from DateTime I can assume that the timezone settings in php.ini are correct. But then why is the time between DateTime and time is different?
I'm not very experienced with PHP myself, but for time() I see GMT. Is GMT the Time zone you are using, or UTC (or something else)? They may be completely seperate.
If i am incorrect, try reading here
Or for more advanced information, Try here.
time() returns the Unix timestamp and is always in GMT:
Returns the current time measured in the number of seconds since the
Unix Epoch (January 1 1970 00:00:00 GMT).
And 21:27:43 GMT is equal to 23:27:43 CEST.
As noted in the comments: Actually the unix timestamp is in UTC, but PHP does not seem to make much of a difference. Unfortunately the documentation is not very enlightening in regards to this.
Related
I have a string $StartDate = "2015-09-23" (should be like yyyy-mm-dd).
Than I make $UdtStart= strtotime($StartDate) that returns 1442980800;
Well if I go to this link it return back "Wed, 23 Sep 2015 04:00:00 +0000".
First, why do we have 04:00:00 added?
Than, if I do this $back=gmdate("Y-m-d H:i:s", ($UdtStart)); I will have "2015-09-26 04:00:00".
What am I missing?
$UdtStart= strtotime($StartDate);
$back=gmdate("Y-m-d H:i:s", ($UdtStart));
Wed, 23 Sep 2015 04:00:00 +0000
Note that +0000 on the end, that means the time is UTC. As per the PHP strtotime() doco:
Each parameter of this function uses the default time zone unless a time zone is specified in that parameter.
The gmdate is for Greenwich Mean Time (and really should be called something like utcdate nowadays), so you're asking for the data in a different foramt from what you gave it.
I'd be willing to bet money that you're in a timezone four hours removed from UTC, which is why you're seeing that.
If you want local time, use date() rather than gmdate(). The gmdate doco states:
Identical to the date() function except that the time returned is Greenwich Mean Time (GMT).
Here's what my server says:
date('c') = 2012-08-09T22:11:13-04:00
time() = 1344564673
Within 10 seconds, here's what http://www.unixtimestamp.com says:
THE CURRENT UNIX TIME STAMP
1344568431 EST (-5 GMT + DST when appropriate)
1344564831 UTC (GMT)
...seconds since Jan 01 1970.
This translates to current server time of 08/09/2012 # 10:13pm in EST.
... and when I type my server's time stamp of 1344564673 into unixtimestamp's converter I get:
TIME STAMP: 1344564673
DATE (M/D/Y # h:m:s): 08 / 09 / 12 # 9:11:13pm EST
... My server's off by almost an hour, right? Or am I missing something? (I don't care if it's off by a few minutes)
Your time appears correct (within a few minutes).
It appears that unixtimestamp.com isn't taking DST into effect, which it is right now in EST. That's why their time is off by an hour from what you get from PHP.
Instead try epochconverter.com which does handle DST.
Just FYI in case you didn't already know: time() always returns timestamps in UTC. When you output them in PHP using date(), the output is reflected in the timezone currently set in PHP. This can be set in php.ini using the date.timezone setting, and you can switch it at runtime using date_default_timezone_set().
Hope that helps.
If I run the following in PHP:
echo mktime(0,0,0,1,1,1970);
the returned value is -3600, not 0 as I expected.
The server is UK based, it's currently 21 Sep (i.e. BST summertime) (though I wouldn't expect this to affect the epoch timestamp) and per php.info: "Default timezone Europe/London".
Setting the daylight saving time flag also, as follows, gives:
echo mktime(0,0,0,1,1,1970,0); (i.e. the correct DST flag, 0 as 1 Jan not DST/BST)
returns -3600
echo mktime(0,0,0,1,1,1970,1); (the incorrect flag - setting 1 Jan as DST)
returns -7200
echo mktime(0,0,0,1,1,1970,-1); (i.e. DST flag not set - left to PHP to decide)
returns -3600
Does anyone know why the epoch would be returned as -3600, not 0, please?
When it was midnight on Jan 1st 1970 in British Summer Time, it was one hour to midnight in Greenwich Mean Time. Try setting the time zone to UTC instead:
date_default_timezone_set('UTC'); // or just change php.ini
mktime() is based on your current timezone. If you want to create a timestamp based on GMT you have to use the gmmktime() function.
gmmktime(0,0,0,1,1,1970)
code on ideone
Resources :
php.net - gmmktime()
I can't seem to get the correct Unix epoch time out of this PHP DateTime object.
$startingDateTime = "2005/08/15 1:52:01 am";
$foo = new DateTime($startingDateTime, new DateTimeZone("America/New_York"));
echo $foo->format('U');
which gives
1124085121
Which is Mon, 15 Aug 2005 00:52:01 GMT -500 (according to EPOCH CONVERTER) but that's incorrect by an hour.
It SHOULD be 1124088721 and spit back at me as Mon, 15 Aug 2005 01:52:01 GMT -500
Any help would be appreciated.
This is likely a DST problem with epoch converter. I used another converter to UTC time and then to America/New_York. I got the right answer given timestamp=1124085121
I've got kind of a tricky question, I already searched every related question on Stackoverflow and neither solved my conundrum, although I think I'm running in circles, so here's the question:
I've got this code:
$val = (strtotime('2010-03-22 10:05:00')-strtotime('2010-03-22 09:00:00'))
This returns correctly $val = 3900 (3600 seconds = 1 hour, 300 seconds = 5 mins)
But doing this:
echo date("H:i",$val)."<br>";
returns 02:05
even doing this:
echo date("H:i",3900)."<br>";
returns 02:05 (just to be naively sure)
Doing this:
echo date("H:i eTO",3900)."<br>";
returns
02:05 System/LocaltimeCET+0100
Which is correct, my timezone is CET and is +1.
What's going on? Is date() correcting the timezone for some reason? Or am I doing anything wrong?
This is happening because using date(, ) returns epoch (00:00:00 01 January 1970 UTC) + the number of seconds in the timestamp. It will localise itself to your timezone, so if you provided it with a timestamp of 0 it would return 01:00:00 01 January 1970 UTC+1.
Yes, it is correcting the timezone. When you do
$val = (strtotime('2010-03-22 10:05:00')-strtotime('2010-03-22 09:00:00'))
, what's stored in $val is a timestamp for 01:05, 1 Jan 1970 UTC. See Wikipedia's article on Unix Time.
If you're working with the difference between two timestamps, I'd suggest using DateTime::diff (requires PHP 5.3).
I did this:
date_default_timezone_set('Europe/Helsinki');
Which is GMT+02:00
and the result was:
03:05 Europe/HelsinkiEET+0200
So, it IS actually correcting for timezone, the explanation now I found to be pretty simple (I had an epiphany): date() counts seconds FROM "1 Jan 1970 GMT" so actually 3900 in my timezone and example is correctly "02:05" from that date...
Self learning +1 -_-'
This is actually the right behavior, because date works with local time, and you are on GMT +1. You give it a timestamp (3900) which is 1/1/1970 1:05 and it just ads 1 to get it to your timezone.
If this is your intended use, than you can just subtract the GMT offset of your machine to get the right value.