date() and gmdate() return different results - php

I run a simple test comparing output of date() and gmdate() and I'm puzzled with results. I understand the difference between functions, however, the server is set to GMT time so I would expect the result to be identical.
I checked timezone setting and they seem to be fine. I suspect this has something to do with daytime savings.
I am right to expect the output to be the same?
if (date_default_timezone_get()) {
echo 'date_default_timezone_set: ' . date_default_timezone_get() . '<br />';
}
echo '<br>Full date '.date("Y-m-d H:i:s T I");
echo '<br>Full date GM '.gmdate("Y-m-d H:i:s T I");
$now = date("Y-m-d H:i:s T I");
$tempDate = $startWeek = time();
$date = new DateTime();
$tempDateU = $date->format('U');
$tempDate = $startWeek = time();
echo '<br>Date: '.date("d/m/y H:i", $tempDate);
echo '<br>GM Date: '.gmdate("d/m/y H:i", $tempDate);
echo '<br>Date: '.date("d/m/y H:i", $tempDateU);
echo '<br>GM Date: '.gmdate("d/m/y H:i", $tempDateU);
Output:
date_default_timezone_set: Europe/London
Full date 2016-04-29 11:35:55 BST 1
Full date GM 2016-04-29 10:35:55 GMT 0
Date: 29/04/16 11:35
GM Date: 29/04/16 10:35
Date: 29/04/16 11:35
GM Date: 29/04/16 10:35

*Warning: date_default_timezone_get(): It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function.*
So you need to set your current timezone to be sure what you are doing:
date_default_timezone_set('Europe/London');
Or
date_default_timezone_set('UTC');
Like mentioned before, Europe/London is not the same as GMT/UTC.
Reminder: GMT does not have DST, it is always the same time.
Europe/London has DST. That is why I in gmdate gives 0

Yes, daylight saving is causing this.
Hence the date() says BST 1 and gmdate() says GMT 0.
Not sure why though, will look into it. Possibly as the time zone is London, so it's in BST.

Related

Ensuring Unix time in PHP

My code assumes that zero represents the beginning of the Unix epoch, 1970-01-01 00:00. I upgraded an installation of PHP and now, all of a sudden, zero represents 1970-01-01 01:00 (as verified with date('Y-m-d H:i', 0)). So apparently there is a time zone matter. I put the same code into a sandbox and got 1969-12-31 16:00. I have several unit tests that are broken as a result of this. Time zones do not and should not come into play here.
How can I ensure that date-time functions such as date() always converts zero to 1970-01-01 00:00 regardless of the time zone setting on the particular installation?
Using gmdate() you'll always get 1970-01-01 00:00 for 0, no matter what timezone your server is in:
<?php
date_default_timezone_set('Europe/Berlin');
echo "Europe/Berlin:\n";
echo "gmdate: ".gmdate('d.m.y H:i', 0) . "\n";
echo "date: ".date('d.m.y H:i', 0) . "\n";
date_default_timezone_set('America/Los_Angeles');
echo "\nAmerica/Los_Angeles:\n";
echo "gmdate: ".gmdate('d.m.y H:i', 0) . "\n";
echo "date: ".date('d.m.y H:i', 0) . "\n";
/* OUTPUT:
Europe/Berlin:
gmdate: 01.01.70 00:00
date: 01.01.70 01:00
America/Los_Angeles:
gmdate: 01.01.70 00:00
date: 31.12.69 16:00
*/
https://3v4l.org/FechC
You need to set the default time zone to GMT if you want to use date() like that. For example:
date_default_timezone_set('GMT');
echo date('Y-m-d H:i [I] [e] [O]',0);
The above will show (no matter what the server has been set to):
1970-01-01 00:00 [0] [GMT] [+0000]
Without the date_default_timezone_set('GMT'), or even set to Europe/London, you will get a different result at different times of the year.
From the PHP manual;
date — Format a local time/date
gmdate — Format a GMT/UTC date/time
The solution is to get the timezone setting, set it aside, change the timezone to UTC, perform the calculations, and reset the timezone to its original setting.
So if my original function looked like this:
public function format($argument = null)
{
// Perform some calculations involving date() and strtotime().
return $result;
}
Now it looks like this:
public function format($argument = null)
{
$timezone = date_default_timezone_get();
date_default_timezone_set('UTC');
$result = ...; // Perform some calculations involving date() and strtotime().
date_default_timezone_set($timezone);
return $result;
}

How to convert GMT time to IST?

Here I am getting GMT Time as follow.
1520489880
Now I am getting This as
$myfinaldate = gmdate("d M H:i", $strtoDate);
So it will returns me GMT time. Now i want to convet it to IST(indian standard time).
where I stuck when I try
//Set timezone to india
date_default_timezone_set('Asia/Kolkata');
echo date('Y-m-d H:i:s');
But this returns me current time not the time that I get in strtotime
You need to pass the timestamp:
date_default_timezone_set('Asia/Kolkata');
echo date('d M H:i', $strToDate); // where $strToDate is 1520489880
Would produce the following value:
08 Mar 11:48
You can convert GMT time to IST time using following ways :
1.
echo date('Y-m-d H:i:s',strtotime('+330 minutes', 0));
2.
date_default_timezone_set('Asia/Kolkata');
echo date('Y-m-d H:i:s');
Changing default timezone is not such a good idea. Use DateTime class, any you don't need to worry about default timezone anymore...
$dt = new DateTime("#$strToDate");
$dt->setTimezone(new DateTimezone('Asia/Kolkata'));
echo $dt->format('Y-m-d H:i:s');
demo

How to store expiration time?

I want to store the expiration time in database. I am using the below code to store expiration time with +1 year.
$cdate = time();
$date = $cdate + 365*24*60*60;
$date = date("Y-m-d H:i:s",$date);
but its not storing the correct time it stores 2014-08-10 07:55:14 but time on storing is 2014-08-10 01:25:14.
Aslo not sure its Am or Pm .
Thanks.
Time/date functions in PHP are using timezones to determine your local time. So if your server is in timezone GMT+6 that means that the date() function will return you the date/time that is 6 hours before GMT.
You can check the date_default_timezone_set() function manual to find out how PHP is selecting your timezone.
To set your timezone, you can use date_default_timezone_set() before calling date function or you can set you php.ini setting date.timezone to your timezone.
For the second part of your question - when formatting time using the date() function the H format character will return 24-hour format of an hour with leading zeros.
try this
<?php
$timezone1 = "America/Los_Angeles";
date_default_timezone_set($timezone1);
$cdate = time();
$date1 = $cdate + 365*24*60*60;
$date = date("Y-m-d H:i:s a",$date1);
echo $date;
$timezone = "Asia/Calcutta";
date_default_timezone_set($timezone);
$cdate = time();
$date1 = $cdate + 365*24*60*60;
$date = date("Y-m-d H:i:s a",$date1);
echo $date;
?>
you can set timezone for your location.And also refer this codepad-FIDDLE
As others have mentioned, it is calculating the time based on your server (local) time.
I suggest you store the time in GMT and then adjust it to your desired timezone as necessary.
You can use strtotime() to calculate 1 year from now (no need to calculate it yourself) and use gmdate() to get the timestamp in GMT.
echo "Next Year in local time: ". date("Y-m-d H:i:s", strtotime("+1 year")) ."\n";
echo "Next year in GMT: " . gmdate ("Y-m-d H:i:s", strtotime ("+1 year")) . "\n";
// Output:
// Next Year in local time: 2014-08-10 15:25:09
// Next year in GMT: 2014-08-10 08:25:09

Linux timestamp to PHP

I have the following timestamp:
1342259667654
which when converted with http://www.epochconverter.com/ gives:
Assuming that this timestamp is in milliseconds:
GMT: Sat, 14 Jul 2012 09:54:27 GMT
Your time zone: 14. juli 2012 11:54:27 GMT+2
And that is the correct time, but when using:
echo date("Y-m-d H:i:s", 1342259667654);
I get the following date:
1904-07-24 10:22:47
How can I get with PHP the exact date out of this time stamp?
Your timestamp needs to be divided by 1000:
echo date("Y-m-d H:i:s", 1342259667654/1000);
$timestamp = 1342259667;
$dt = new DateTime("#$timestamp"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s');
You can also do it this way.
The value 1342259667654 is actually in miliseconds, while PHP's date() function is unable to handle miliseconds value. Hence the weird output.

How to convert time like 2 pm or 3 am to HH:MM:SS format in php?

How can I convert times like 2 pm or 3 am to HH:MM:SS format in php?
Like this:
$date = '2pm';
echo date('H:i:s', strtotime($date));
Result:
14:00:00
And:
$date = '2am';
echo date('H:i:s', strtotime($date));
Result:
02:00:00
More Info:
date
strtotime
Update:
To convert it back:
$date = '14:00:00';
echo date('HA', strtotime($date));
Result:
14PM
(this should be a comment, but it's too long)
The reason Sarfraz's solution is incomplete is because it doesn't account for DST transaitions. During DST transitions, some hours may not exist.
Consider the timezone is Europe/Lisbon and we're in March 28th 2010, when DST kicked in.
When we hit 1am, we change from UTC+0 to UTC+1, i.e., we skip 1 hour. Example:
date_default_timezone_set("Europe/Lisbon");
$date = '2010-03-28 1am';
$date2 = '2010-03-28 1:30am';
echo date('H:i:s', strtotime($date)),"\n";
echo date('H:i:s', strtotime($date2)),"\n";
gives
02:00:00
02:30:00
Therefore, Sarfraz solution will fail unless when you say you want to convert 1am to 01:00, these times always refer to the current day in the server's timezone.

Categories