Converting a Time from a database to BST with PHP - php

So I've trying all sorts of combinations to get a date from my database (using Wordpress) to display in British Summer Time and I cannot get anything to work.
Is there any simple solution that can take the date string and make sure that in Summer Time in the UK it's an hour on from UTC time?
$classJson = $class->info;
$classJsonAsArray = json_decode($classJson, TRUE);
$classStartDate = strtotime($class->periodStart);
$classStartTime = date('H:i',$classStartDate);
So currently $class->periodStart returns: 2022-04-06 08:30:00
The time of that event should be 9.30am
All I need it to do is display the correct time, as at the moment, on the front end it displays as 8.30am.

DateTime handles timezones quite well.
$dateStringInUtc = '2022-04-06 08:30:00';
$date = new DateTime($dateStringInUtc, new DateTimeZone('UTC'));
$date->setTimezone(new DateTimeZone('Europe/London'));
echo $date->format('Y-m-d H:i:s'); // will output 2022-04-06 09:30:00

Working with DateTime and timezones like in the accepted answer is the better way.
But it also works with strtotime if the timezone is appended to the date string. Date then returns the local time for a timestamp.
$utcDate = '2022-04-06 08:30:00';
echo date('Y-m-d H:i:s',strtotime($utcDate.' UTC'));

Related

Facebook SDK returning incorrect times for events

I have a website that displays events from the website owner's Facebook page. A few weeks ago, someone noticed that the event times are showing up wrong on the website, but they've been correct for a couple years. So basically I'm trying to figure out what the problem is.
Here's an example
Event X has a start time timestamp of 2017-12-18T17:00:00-0500. That date is correct, and that time - 1700, or 5:00 - is the correct time.
So I have this code to convert the timestamp to something I can display
$start_time = date('g:i a', strtotime($event['start_time']));
This returns a time of 10:00 PM
I have the same problem with the end time not converting correctly.
I'm using this code to convert the date (using the same timestamp above):
$start_date = date('l, F j, Y', strtotime($event['start_time']));
This returns the correct date, which confuses me even more because if the date converts correctly, how does the time not convert correctly?
Can someone please help me get the time to convert so I can get these events back on the website?
It is different timezone problem.
You have a datetime with timezone -05:00 but, date('P') output, your server timezone is +00:00. You can use DateTime class to convert the datetime to the timezone your desired.
// convert time to datetime instance
$timestamp = strtotime($event['start_time']);
$datetime = new DateTime;
$datetime->setTimestamp($timestamp);
// set timezone to US/Eastern, Eastern Standard Time (EST), UTC -5
$datetime->setTimezone(new DateTimeZone('US/Eastern'));
// output datetime format
$datetime->format('g:i a');
$datetime->format('l, F j, Y');

PHP date timestamp timezone not converted properly

So I have this code:
$timestamp = 1414708099;
echo $timestamp;
$date = date_make_date($timestamp, 'UTC', 'datestamp');
date_timezone_set($date, timezone_open('America/New_York'));
$timestamp = $date->format('U');
echo '<br>';
echo $timestamp;
which is supposed to convert the timezone of the initial timestamp from UTC to new york.
but then this ends up printing
1414708099<br>1414708099
hence the timezone didnt change...
what did I do wrong?
btw it also uses Drupal 6 date_api.module: http://drupalcontrib.org/api/drupal/contributions!date!date_api.module/function/date_make_date/6
As per comments
A timestamp is always UTC. You can't apply a time zone to a timestamp - consider its timezone as 0. Whatever you do, it stays 0. You asked for a date formatted with U - manual states this:
U: Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
You can't get seconds from Unix Epoch for New York. That number is the same for any location in the world.
Now, had you formatted that date using, say, $date->format('Y-m-d H:i:s') then you would get correctly formatted time with the timezone offset for New York.
Long story short - there is no problem whatsoever here. It all works as intended.

Datetime php conversions and usage with timezone

I am trying convert a utc time stored date to another time zone but i cant seem to get it right.
I have a time :
date1 = new DateTime('first day of the month');
date1.setTime(0,0,0); // Since using the first day of the month seems return the current time with different date
The default DateTime timezone is in UTC. The time i want to make reference is in 'Europe/Amsterdam' timezone. Any way i cant get the time in 'Europe/Amsterdam' timezone to be equivalent to the first day of the month time in UTC? (Uh, sorry my question was confusing.. let me just give an example to be clear). Im trying to query from a db.
If UTC date time is June 01, 2013. 00:00:00
I want to get get May 29, 2013 19:55:00.
I tried getting the difference between the two declared times with different timezones to get the time that i wanted but it seems it didnt work :(
My Edit/ Clarification:
If use this code:
$date1 = new DateTime('first day of the month');
$date1.setTime(0,0,0);
print_r($date1->format('Y-m-d H:i:s'));
I would get:
2013-06-01 00:00:00
Then if i use timezone:
$date1->setTimeZone(new DateTimeZone('Europe/Amsterdame'));
print_r($date1->format('Y-m-d H:i:s'));
I would get: (This is just a sample output):
2013-06-01 03:00:00
Because of time difference. Want i want to get is like the reverse: I want to get the datetime that when converted 'UTC' timezone i would get this: 06-01-2013 00:00:00 time. So my preffered output is : 2013-05-29 21:00:00 ...
You can do in an OOP way like so.
$date = new DateTime('2000-01-01 00:00:00', new DateTimeZone('Europe/Amsterdam'));
echo $date->format('Y-m-d H:i:s P') . "\n";
To set the default date in PHP, you can either set it in your ini file or in a PHP file like so:
date_default_timezone_set('Europe/Amsterdam');
Then to format the date, refer to http://www.php.net/manual/en/function.date.php for formatting.
In your case this would be:
date('j M Y' time());
Where j = day, M = month and Y = year.

What's the best way to convert a date and time into a timestamp using php?

I need to convert a date and time (GMT) into a timestamp with php. The following code shows what I'm currently using:
<?php
$date="2012-06-29 10:50";
$timestamp = strtotime($date);
echo $timestamp;
?>
However, when I test the timestamp in an online convertor (http://www.epochconverter.com), the resulting date is 29th June 2012, 8:50 GMT, or 2 hours previous. Is it possible that the strtotime() function isn't completely accurate and is just an estimate of the time? If so, are there better methods I could use for getting the exact time?
Thanks.
strtotime assumes that you are converting a string in your server's local time, so if the servers time zone is two hours out the result will be as wll.
The comments in the manual suggest a couple of solutions, you can append UTC to your date:
$timestamp = strtotime($date.' UTC');
Or you can change the default timezone for the script (this will apply to all other time functions!):
date_default_timezone_set('UTC');
$timestamp = strtotime($date);
As a final alternative, you could try date_create_from_format which allows you to specify what exactly the format your string is:
$datetime = date_create_from_format('Y-m-d H:i', $date, new DateTimeZone('UTC'));
$timestamp = date_format($datetime, 'U');
// Alternatively (thanks Herbert) - 5.3+ only
$timestamp = date_timestamp_get($datetime);

Trying to understand Timestamps and Timezones in PHP

I have a PHP application that needs to support timezones. Now I am storing date and time values in the database as integers. So here is what I do.
// now create the DateTime object for this time and user time zone
$dtime = new DateTime($date_time, new DateTimeZone($time_zone));
$timestamp = $dtime->format('U');
What I don't understand is even if the person is in New Zealand (Pacific/Auckland) and the application stores this date: 24-12-2010 00:00:00 using the above code the value comes back as: 1293102000
Then using the exact same date/time and setting the time zone to London (Europe/London) I get this timestamp:1293148800
Why are the timestamps different? I thought the timestamp would be the same number of seconds as its the same date from the Unix Epoch?
If they are different that means when someone is searching the database for all records between 24-12-2010 00:00:00 and 24-12-2010 23:59:59 I need to use the users timezone for the creation of the timestamps and I must use the timezone to firstly create the timestamps.
Help!
Because timezone for New Zealand is +1300 UTC, where London is zero UTC, 13 x 3600seconds = 1293148800-1293102000
Since you already store as integer, you should always use zero UTC skip timezone when you construct $dtime
For testing
# date_create is procedural style
$obj = date_create('24-12-2010 00:00:00', new DateTimeZone('europe/london'));
echo $obj->format('U');
$obj = date_create('24-12-2010 00:00:00');
echo $obj->format('U');

Categories