Displaying actual time using iso 8601 - php

I m referring to this: Converting ISO 8601 format to d M Y in PHP
I tried this same as this: echo date("d M Y H:i:s", strtotime($time));
But time is not shown as saved in database. Its showing few hours difference.
Database has: 2016-03-20T23:30:51+00:00
With above php echo i get: 21 Mar 2016 00:30:51
Where as it must be 20 Mar 2016 23:30:51
Above example shows additional of 1 hour y?
I tried using this format to display hour & minute but time shown is wrong. Time display has few hours difference. Why is this so?

Your date format 2016-03-20T23:30:51+00:00 reveals a GMT DateTime (side note: the same TimeZone used by php/unix timestamps).
So when you write:
echo date( "d M Y H:i:s", strtotime( $time ) );
You obtain yet the correct date converted in your system TimeZone.
You can use DateTime class to perform more operations with dates:
$date = new DateTime( $time );
echo $date->format("d M Y H:i:s");
will print the date in original (GMT) format.
$date->setTimezone( new DateTimeZone('Europe/Berlin') );
echo $date->format("d M Y H:i:s");
will print the date in 'Europe/Berlin' Timezone.
Side note: saving dates in ISO 8601 UTC/GMT is actually the best choice.
Read more about DateTime
Read more about DateTimeZone

Your system is using its local timezone instead of UTC, whereas the database is in UTC. If you wish to display in UTC, you can use http://php.net/manual/en/function.date-default-timezone-set.php to set the timezone to UTC before calling strtotime and date.

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 does not match database?

This is the coding and it is echoing with the right format but the fact that the date is wrong when it prints out.
Output: 31 Dec 1969 19:33
Database Timestamp 2016-05-20 21:53:17
<?php
date_default_timezone_set('ECT');
$timestamp = 1456778973;
echo date('d M Y H:i',$row['timestamp']);
?>
and i have tried doing the date in different ways and still the same result
In the code sample you posted, $row['timestamp'] has not been set, so the date is constructed with timestamp 0, also known as epoch, or the date that is being echoed.
If you change it as follows, it should work fine:
<?php
date_default_timezone_set('ECT');
$timestamp = 1456778973;
echo date('d M Y H:i', $timestamp); ?>
Side note:
Time zone ECT is not a valid time zone code in PHP. If I assume correctly that you mean european central time, you would have to specify CET instead.
ECT doesn't exist as a valid TimeZone, did you mean CET perhaps?
The correct way to do this is using the DateTime class, i.e.:
$date = new DateTime();
$date->setTimestamp(1456778973);
$tz = new DateTimeZone("America/Denver");
$date->setTimezone($tz);
echo $date->format('d M Y H:i');
PHPFiddle Demo
Note:
Dates should always be stored in DB as UTC (timestamp aka unix time), then you can add or subtract the timezone offset using the DateTime class.
Would you know what the offset would be for mountain standard time?
Mountain Time: America/Denver
Mountain Time (no DST): America/Phoenix
List of Supported Timezones

Calculate Future PHP Date in Specific Format (dd month yyyy hh:mm:ss)

Extreme PHP newbie here - I am trying to create a PHP variable that will be "CURRENT DATE + 7 Days"
Something like :
date('D-m-y H:i:s', strtotime(DateTime("+7 day"))
However, I need it to output in a format like this: "30 November 2015 09:00:00"
Any ideas?
Thanks in Advance!
You can check the manual for valid date formats and change your format string.
You're basically looking for date('j F Y H:i:s', strtotime("+7 day"))
Personally, I recommend working with DateTime if you're storing this in a variable and working with it, because it becomes more convenient to extract the formatted date from the object at your conveience any time without having to go back through date and strtotime each time. Also there are numerous other benefits like not losing timezone information during conversion or having to change global timezones that effect the conversion, etc...
Example
$date = new DateTimeImmutable; // today's date
echo $date->modify('+7 days')->format('j F Y H:i:s'); // 7 days from today
echo $date->modify('-7 days')->format('j F Y H:i:s'); // 7 days ago

Showing wrong time while changing the format of date

I'm changing the date format and it shows me wrong format
echo $punch_in_time;
// Prints 2013-09-09 11:40:00
echo $new_date = gmdate('D, M-d-Y h:i a',strtotime($punch_in_time));
// Prints Mon, Sep-09-2013 09:40 am (Notice the wrong time)
I also tried to set the time zone before displaying the time, but no effect.
I don't know why this is happening, it must show my time as Mon, Sep-09-2013 11:40 am instead of Mon, Sep-09-2013 09:40 am.
besure to read the description/manual before you use a function.
it says "Format a GMT/UTC date/time" in the description of gmdate(), which means it is assuming the date you entered is in the local time zone (judging from the time difference GMT+2 ?) gmdate then convert it to a date format in GMT+0 time zone.
to make sure the timezone* is consistance between both input and output, use date() instead.
*this will convert the datetime to your local timezone, which might not be what you need.
echo $new_date = date('D, M-d-Y h:i a',strtotime($punch_in_time));
gmdate() identical to the date() function except that the time returned is Greenwich Mean Time
use this instead
echo $new_date = date('D, M-d-Y h:i a',strtotime($punch_in_time));

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