PHP timestamp an hour slow - php

I am working on an ancient system written in vanilla PHP.
I am having some time conversion issues.
In the below example, I an expecting both the timestamp and the "pretty date" to reflect one another:
<?php
date_default_timezone_set( 'Europe/London' );
$timestamp = 1509062400; //Friday, October 27, 2017 12:00:00 AM
$date = new DateTime();
$date->setTimezone( new DateTimeZone( 'Europe/London' ) );
$date->setTimestamp( $timestamp + 3601 );
$date->setTime( 0, 0 );
echo $date->getTimestamp();
echo '<br/>';
echo $date->format( 'Y-m-d H:i:s' );
However, when running this script on the server, the result is the following:
Timestamp: 1509058800
Pretty Date: 2017-10-27 00:00:00
The pretty date represents 27th Oct 00:00, but the timestamp (according to epochconvertor) represents 26th Oct 23:00.
I am not sure why the timestamp is an hour slow. Can anyone help?

If you're in London, then your timestamp is likely in British Summer Time. The epoch is going to be in GMT. BST is one hour ahead of GMT.

Related

Change UTC0 to UTC +1/+2

I got a timestamp from my SQL-database: $DBdata = '2019-10-10 12:25:59', this date is UTC+0. Well i live in Denmark where we have the following UTC.
Central EU time winter (UTC+1)
Central EU summertime (UTC+2)
These UTC's changes from summer to winter different dates each year.
Summer time
• 2019 - The night between Saturday 30 March and Sunday 31 March
• 2020 - The night between Saturday 28 March and Sunday 29 March
• 2021 - The night between Saturday, March 27 and Sunday, March 28
• 2022 - The night between Saturday 26 March and Sunday 27 March
• 2023 - The night between Saturday, March 25 and Sunday, March 26
Winter time
• 2019 - The night between Saturday, October 26 and Sunday, October 27
• 2020 - The night between Saturday, October 24 and Sunday, October 25
• 2021 - The night between Saturday, October 30 and Sunday, October 31
• 2022 - The night between Saturday 29 October and Sunday 30 October
• 2023 - The night between Saturday 28 October and Sunday 29 October
Would it be possible to make an if-statement that changes these UTC's, with the right +1/+2?
Maybe something like this:
$Winter= gmdate('Y-m-d H:i:s', strtotime("WinterUTC"));
$Summer= gmdate('Y-m-d H:i:s', strtotime("SummerUTC"));
if (($$DBdata>= $Winter) && ($$DBdata<= $Summer)){
$gmt_dateWinter = gmdate('Y-m-d H:i:s', strtotime($date + '+ 2 hours') );
}else{
$gmt_dateSummer = gmdate('Y-m-d H:i:s', strtotime($date + '+ 1 hours') );
}
I don't know if this even is a smart way to do this. If there is another more smart way to do it please lead me in another direction.
Thanks!
With the DateTime class you can easy convert a date/time to any other time zone.
function convertTimeZone($strDateTime, $sourceTimeZone, $targetTimeZone){
return date_create($strDateTime, new DateTimeZone($sourceTimeZone))
->setTimeZone(new DateTimeZone($targetTimeZone))
->format('Y-m-d H:i:s');
}
example with a daylight saving time
$localTime = convertTimeZone('2019-10-10 12:25:59', 'UTC', 'Europe/Copenhagen');
echo $localTime."<br>";
returns:
2019-10-10 14:25:59
example wintertime
$localTime = convertTimeZone('2019-02-10 12:25:59', 'UTC', 'Europe/Copenhagen');
echo $localTime."<br>";
returns:
2019-02-10 13:25:59
Note: Solutions based on stringtotime are not recommended because of known issues and limitations on some systems.
You can use gmdate and date("I") to generate the date considering the timezone:
gmdate: Format a GMT/UTC date/time
$DBdata = '2019-10-10 12:25:59';
$timezone = +2; // (GMT +2:00) CEST (European Summer Time)
$GMdate = gmdate("Y-m-d H:i:s", strtotime($DBdata) + 3600*($timezone + date("I")));
echo $GMdate;
returns:
2019-10-10 22:25:59
From the date documentation:
I (capital i) Whether or not the date is in daylight saving time: 1
if Daylight Saving Time, 0 otherwise.
Thats simple change in php script default timezone and then display time:
https://www.php.net/manual/en/function.date-default-timezone-set.php
$time = date('H:i:s', time());
$ok = date_default_timezone_set('America/Los_Angeles');
echo $tz = date_default_timezone_get();
$h = new DateTime();
echo $h->format('H:i:s'); // curr time
$o = new DateTime($time);
echo $o->format('H:i:s'); // time

Php unix timestamp with strtotime

I start learn PHP and is very clear I make some error here, I try obtain Unix timestamp of specific hour and minutes of the day:
<?php
date_default_timezone_set('America/Argentina/Buenos_Aires');
$data = new DateTime();
$datafmt = $data->format('Y-m-d');
echo strtotime($datafmt,'18:30:00');
?>
The code return 1554951600 and is equal to:
GMT: Thursday, April 11, 2019 3:00:00 AM
Your time zone: Thursday, April 11, 2019 12:00:00 AM GMT-03:00
This is wrong, timestamp should be:
1555018200 is equal to:
GMT: Thursday, April 11, 2019 9:30:00 PM
Your time zone: Thursday, April 11, 2019 6:30:00 PM GMT-03:00
What I doing wrong?
Fixed!
echo strtotime($datafmt. '18:30:00');
, instead . that is my error!
You do not need strtotime() at all. DateTime class is a replacement and is more powerful. Just pass the time to the constructor or set it with the method setTime()
<?php
date_default_timezone_set('America/Argentina/Buenos_Aires');
$data = new DateTime('18:30:00');
// Alternative ways to set the time of the DateTime object
// $data->setTime('18', '30', '00');
// $data->setTime(...explode(':', '18:30:00'));
$datafmt = $data->format('U'); // U means UNIX timestamp
echo $datafmt;

php unix to date not work correctly

i have a unix time 1410938094654
I want to convert this to date time format
$dt = new DateTime('#1410938094654');
$dt->setTimeZone(new DateTimeZone('Asia/Tehran'));
echo $dt->format('F j, Y, g:i a');
But the wrong time will returned
06-11-2000 00:30:54
correct time is GMT: Wednesday, September 17, 2014 7:14:54.654 AM
also my php.ini
date.timezone = "Asia/Tehran"
gmdate also returns the same date (wrong date)
DateTime accepts unixtime in seconds, and you have it in milliseconds, so you must divide timestamp to 1000 before creating new DateTime object.
$dt = new DateTime('#1410938094654');
gives: November 6, 46680, 12:30 am
$dt = new DateTime('#1410938094');
gives September 17, 2014, 7:14 am
Convert it to seconds before using.

PHP DateTime converting issue

I have an issue with converting DateTime from Moscow timezone to New York timezone. Here is my test script:
$year = 2015;
$month = 3;
$tzMoscow = new DateTimeZone('Europe/Moscow');
$tzNewYork = new DateTimeZone('America/New_York');
$startDate = DateTime::createFromFormat('Y-n-d', "$year-$month-01", $tzMoscow);
echo $startDate->format('Y-m-d H:i:s')."\n"; // 2015-03-01 16:16:05
$startDate = DateTime::createFromFormat('Y-n-d', "$year-$month-01", $tzNewYork);
echo $startDate->format('Y-m-d H:i:s') . "\n"; // 2015-03-01 09:16:05
$startDate->setTimezone($tzMoscow);
echo $startDate->format('Y-m-d H:i:s') . "\n"; // 2015-03-01 17:16:05
Third output is incorrect, time should be 16:16:05. Am I doing something wrong or is this a bug in php?
I think I've got it. The problem is that you're not specifying a time of day, so createFromFormat is using "the current system time":
If format does not contain the character ! then portions of the generated time which are not specified in format will be set to the current system time.
Now what does it mean to use "the current system time" to create a time in the New York time zone, when that time zone has changed UTC offset between the specified date (March 1st) and the current date (March 14th)? It was UTC-5 on March 1st, and it's now UTC-4 due to daylight saving time.
I believe PHP is taking the current time of day in the specified time zone (9:16 at the time you ran that code) and then using that as the time of day on the specified date. So we end up with March 1st 2015, 09:16 New York time - or March 1st, 14:16 UTC, which is indeed March 1st 17:16 Moscow time.
That's not the same as the current time of day in Moscow at the time that you ran the code, which was 16:16.
Basically, you should try not to do this - or expect problems like this to happen. Think about what time of day you're really trying to represent, bearing in mind that within a particular time zone, offsets change over time. I can't really advise you on what your code should be, because we don't know what you're trying to achieve - but using the current time of day for a different date can definitely cause this problem.
I also believe this is an issue of DST, changing in New York time zone on 8th of March. I extended your date formats to Y-m-d H:i:s U I to include the unix timestamp and the DST value.
The output is now as following:
2015-03-01 19:07:17 1425222437 0
2015-03-01 11:07:17 1425226037 0
2015-03-01 20:07:17 1425226037 0
As you can see, the unix timestamp is already different between both created DateTime objects.
Now when I specify a concrete time as
$startDate = DateTime::createFromFormat('Y-n-d H:i', "$year-$month-01 00:00", $tzMoscow);
as well as
$startDate = DateTime::createFromFormat('Y-n-d H:i', "$year-$month-01 00:00", $tzNewYork);
the output changes to:
2015-03-01 00:00:00 1425153600 0
2015-03-01 00:00:00 1425186000 0
2015-03-01 09:00:00 1425186000 0
On the other hand, if I change $month to 4 (where New York uses DST), I get the following output:
2015-04-01 19:08:35 1427900915 0
2015-04-01 11:08:35 1427900915 1
2015-04-01 19:08:35 1427900915 0
What do these results mean?
The conversion between New York and Moscow time zone works correct in all cases, as you can judge from the unix timestamp being identical. Also, "2015-03-01 00:00" are obviously different timestamps for Moscow and New York, because they depend on the concrete time zone.
So I think your code is correct and there is no bug in php. However, the "current" times in New York and Moscow differ due to the DST switch between 1st of March and today (14th of March).
So while Jon's answer already explained the theory (I don't want all the credit, he was first), maybe someone will still find some concrete examples useful.
You can use this function, for change between timezones
function changeTimezone($time, $currentTimezone, $timezoneRequired, $FormtsTime = 'Y-m-d h:i:s')
{
$dayLightFlag = false;
$dayLgtSecCurrent = $dayLgtSecReq = 0;
$system_timezone = date_default_timezone_get();
$local_timezone = $currentTimezone;
date_default_timezone_set($local_timezone);
$local = date($FormtsTime);
date_default_timezone_set("GMT");
$gmt = date($FormtsTime);
$require_timezone = $timezoneRequired;
date_default_timezone_set($require_timezone);
$required = date($FormtsTime);
date_default_timezone_set($system_timezone);
$diff1 = (strtotime($gmt) - strtotime($local));
$diff2 = (strtotime($required) - strtotime($gmt));
$date = new DateTime($time);
$date->modify("+$diff1 seconds");
$date->modify("+$diff2 seconds");
if ($dayLightFlag) {
$final_diff = $dayLgtSecCurrent + $dayLgtSecReq;
$date->modify("$final_diff seconds");
}
$timestamp = $date->format($FormtsTime);
return $timestamp;
}

How do I get midnight of current day in a specified timezone in PHP?

I have a server in New York, and a "client" in California which expects that server to figure out midnight of the current day in California, which is not the same as New York's for three hours out of the day.
I have only been able to calculate midnight for the current day as is seen by New York, but I need to be able to calculate midnight of the current day for California (or any other arbitrary timezone)
I do have access to the timezone's offset from UTC.
$tzOffset = "-700"; // Timezone offset from UTC (in this case, PDT)
The (semi-working) method that I'm using right now is
strtotime("00:00:00 " . $tzOffset);
On Jun 25 at 10:00 PM PDT, this method is returning Jun 26 at 12:00 AM PDT as midnight, when it should be returning Jun 25 12:00 AM PDT.
// create time from string
$date_time = new Datetime('midnight', new Datetimezone('America/Los_Angeles'));
// string representation of time in America/Los_Angeles
echo $date_time->format('Y-m-d H:i:s') . PHP_EOL;
$date_time->setTimezone(new Datetimezone('America/New_York'));
// string representation of the same time in America/New_York
echo $date_time->format('Y-m-d H:i:s') . PHP_EOL;
http://php.net/manual/en/datetime.formats.php
Create datetime object in one timezone
$dt = new Datetime('2014-06-25 00:00:00', new Datetimezone('America/New_York'));
Change it to another timezone
$dt->setTimezone(new Datetimezone('America/Los_Angeles'));

Categories