Datetime php conversions and usage with timezone - php

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.

Related

Converting a Time from a database to BST with 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'));

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');

Shifting unix time stamp according to timezone

I know this question has been answered many many times. I came in to a solution to solve this and its goes like this. I store all time stamps for each post in UTC on the server. Now i need to display the time stamp for a given timezone. I do this:
$tz : requested timezone
$ts : timstamp on db
$newts : new timestamp
$datetime = date('m/d/Y g:i a', $ts);
$dt = new DateTime($datetime, new DateTimeZone('UTC'));
date_default_timezone_set(trim($tz));
$newts = $dt->format('U');
date_default_timezone_set('UTC');
However the resulting time stamp is 60~ seconds higher than what is should be.
What am i doing wrong?
You're close, all you need to do is create the original DateTime object based on the timestamp/server timezone then set the new timezone and print the result, like so:
$datetime = new DateTime('#'.$ts, new DateTimeZone('UTC'));
$datetime->setTimezone(new DateTimeZone($tz));
print $datetime->format('m/d/Y g:i a');
The unix timestamp will be the same regardless of the timezone (it is TZ agnostic). The offset occurs when displaying it for different time zones. This is you can test this by printing the unix timestamp for each different timezone (they will be the same).
The U format gives you the number of "seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)". That number does not depend on where on earth you are. So even if it might be 14:59 at your place while it's 10:24 at my place, the number of seconds since January 1 1970 00:00:00 GMT is the same at both our places. A "time stamp for a given timezone" does not make sense.

Why is getTimestamp() affected by setTimezone()

I've been struggling quite a while with PHP's DateTime classes. My understanding is that a UNIX-timstamp is always in UTC, regardless of the timezone.
That's why I am quite confused with this code sample.
$date1 = new DateTime("#1351382400"); // Sun Oct 28 2012 02:00:00 GMT+2 (DST)
var_dump($date1->getTimestamp()); //prints: 1351382400
$date1->setTimezone(new DateTimeZone("Europe/Stockholm"););
var_dump($date1->getTimestamp()); //prints: 1351386000
As you can see setTimezone() changes the result of getTimestamp().
Is it expected that setTimezone() affects getTimestamp()?
The amount that you're off is 3600 seconds, or 1 hour.
I think that what you're seeing is because the date you picked is the end of Daylight Savings Time in Stockholm. If you use a different date, you don't get that effect:
$now = time();
echo " now: $now\n";
$date1 = new DateTime("#{$now}");
echo " date1 here: {$date1->getTimestamp()}\n";
$date1->setTimezone(new DateTimeZone("Europe/Stockholm"));
echo "date1 Stockholm: {$date1->getTimestamp()}\n";
Output:
now: 1352321491
date1 here: 1352321491
date1 Stockholm: 1352321491
I'm not sure if this is a bug or not, but it doesn't happen if you don't pick a date on which DST is changing.
Yes, unix timestamp is the current time as per the date object or your current machine time from Epoch.

PHP: Get Time for Current Region

I have a timezone of the user(he chooses it from a list)
I have a time in UTC(not current time)
So I need something like GetTimeForRegion(time, timezone) for PHP. Is there such functions or libraries or services?
you can use DateTime::setTimezone(). If your UTC date is an UNIX timestamp, you can use some code like this :
$date = new DateTime();
$date->setTimezone(new DateTimeZone('UTC'));
$date->setTimestamp(1297869844);
$date->setTimezone(new DateTimeZone('Europe/Paris'));
echo $date->format('Y-m-d H:i:s');
// Will print 2011-02-16 16:24:04
date('r') or date('c') may help you.
echo date('r') prints Thu, 16 Feb 2011 16:01:07 +0200
echo date('c') prints 2011-02-16T16:01:07+02:00
You need to look at the Date/Time API in PHP. I strongly advise you to stay away of gmdate and older date functions in php.
In your case, you should ask the user for its Olson based time zone.
The code of Artefact2 will do the trick.
please write this instead :
$date = date("Y-m-d H:i:s" , time());
so Y it means year m means month d means day
H get hours from 0 - 24
h get hours from 0 to 12
i get minutes
s get seconds

Categories