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
Related
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.
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
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.
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.
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));