Why is PHP date() adding +1 hour in diff calculation? - php

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.

Related

Using strtotime() PHP and revert back trough gmdate() is not returning same date

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).

date() function outputting wrong result - PHP

Here's what i did -:
Generated the UNIX_TIMESTAMP result in mysql. It came out
1360756718 seconds.
Since I am in GMT +5.30, got the number of seconds in 5 hrs and 30 mins. Result was 19800 seconds
Ran this function -> date('j M Y H:i:s',(1360756718+19800)).
But unfortunately, the answer was 1 hour ahead of original time. And I mean exactly 1 hour. Result was 13 Feb 2013 18:28:38 which should have been 13 Feb 2013 17:28:38.
Where am I worng?
Well, you are in +5:30 but with daylight savings time, we still are 1 hour back. so you now are only +4:30
I think your timezone is set incorrectly. You need to dig around your Apache/PHP/Mysql settings to find out what it is set to.
echo date( 'd.m.Y H:i:s', '1360756718' ); // 13.02.2013 13:58:38
echo date( 'd.m.Y H:i:s', ( '1360756718'+19800 ) ); // 13.02.2013 19:28:38
For me this came out as planned. Are you sure that you started with the right time? As some already mentioned, the problem most likely lies in your timezone or daylight savings time.
Please check your current timezone first.
Here you can check all supported timezones as well
You could try adding:
ini_set('date.timezone', 'My/Timezone');
Since php 5.1 it's also possible to do:
date_default_timezone_set ( string $timezone_identifier )
http://www.php.net/manual/en/function.date-default-timezone-set.php
To set the time to your own timezone. The timezones that are available for php are here:
http://www.php.net/manual/en/timezones.php
the other answers say you should check and correct your timezone settings.
I would do that because it's more correct (regarding to daylight saving time and so on).
If you want to have the easy way try gmdate function:
string gmdate ( string $format [, int $timestamp = time() ] )
This one will always calculate in GMT - you don't have to check the server settings.
(However it will ignore Daylight Saving Time too.)

PHP strtotime for June returns July

I'm stumped as to why the following PHP strtotime function returns '07' as the month number, rather than '06' when $monthToGet = 'June':
$monthToGet = $_GET['mon'];
$monthAsNumber = date('m', strtotime($monthToGet));
From searching, it appears it may be due to default date parameters (in this case the day and year) as I haven't specified them. Would that be the cause?
Any suggestions appreciated!
TL;DR
You are right
echo date("m", strtotime("June"));
-> 07
However, this does work:
echo date("m", strtotime("1. June 2012"));
-> 06
The problem explained
Today is 31. July 2012 and since you provide only a month, the current day and current year are used to create a valid date.
See the documentation:
NOTE
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.
Alternatives
You could use date_parse_from_format() or strptime() to achieve what you want with a slightly different approach.
(Thanks to johannes_ and johann__ for their input)
Fixed with :
$monthToGet = '1 '. $_GET['mon'];
But I still don't get why, since "m" is a valid date format
Today is 31 Jul. So a strtotime with only "June" is interpreted as 31 June => 1 July.
In fact:
echo date("Y-m-d",strtotime("January")); // 2012-01-31
echo date("Y-m-d",strtotime("February")); // 2012-03-02
of course... only today 31 Jul 2012 :) Tomorrow all will works.
You're lucky because you found this bug just today ;)

how this code work ?? and what is the right code

This is my current code that doesn't seem to work correctly.
echo date("h:i", 1*60*60) ; /* 1*60*60 mean 1 hours right */
The result is 03:00 when it should be 01:00.
Where is this going wrong?
i want to do this
1- employee come in time admin select it
2- employee come - leave saved in mysql as timestamp
Now I'm trying to make admin see how many hours user late
mean
user date time default late
user1 11-09-2011 09:10 09:00 10 min
user1 12-09-2011 08:00 09:00 -60 min
If you output just date("Y-m-d H:i:s",0) you should see that it's not 1970-01-01 00:00:00 as it should be. It's because date is affected by your local timezone. For example I'm in GMT +3, and date("Y-m-d H:i:s") gives me 1970-01-01 03:00:00.
So the answer is you are not in GMT timezone (probably in GMT+2), and date is affected by it.
UPDATE
The following code outputs 1970-01-01 00:00:00, so it's definitely time zones.
date_default_timezone_set('UTC');
echo date("Y-m-d H:i:s", 0);
Hovewer, I can't see any mention about it in PHP's date manual.
The problem is due to your timezone (looks like GMT+2).
Date calculations in PHP are based on the configured server timezone. For example mine shows
$ php -r 'echo date("h:i", 3600), PHP_EOL;'
11:00
The second argument in date() function must be UNIX timestamp, seconds passed from Jan 1 1970. You need to make time according to that value.
You have probably not setup time zones, which should produce a PHP warning or notice if omitted.
It occurs to me that what SamarLover think he wants is
gmdate("h:i", 1 * 60 * 60);
Err, if I'm right, date()'s second param is a unix timestamp, so the seconds since 1970.
You have to get time() and add 60*60 to it.
echo date("h:i", time()+60*60); // get current timestamp, add one hour

php mktime for epoch returns -3600, not 0

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()

Categories