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));
Related
I have a date in this format "Wed, 26 Oct 2022 16:11:30 -1100", need to convert it to UTC time and in a format so it can be inserted into a MySQL database.
The date was pulled from an email using "$headerInfo->date;". I don't see any way to receive the date any different.
Every way I try to do the conversion is painful and brute force.
Is there an elegant, or not painful way to do this?
TIA.
Been trying regex but it doesn't handle converting the month into digits, then you have the UTC offset (time zone) to work with.
Parse the date/time string using DateTimeImmutable::createFromFormat, set the timezone to UTC then format it to MySQL's datetime literal syntax
$dt = DateTimeImmutable::createFromFormat('D, j M Y H:i:s O', $dateString);
$mysqlFormat = $dt->setTimezone(new DateTimeZone('UTC'))->format('Y-m-d H:i:sP');
Demo ~ https://3v4l.org/G7RBG
<?php
//just use strtotime
echo date('Y-m-d H:i:s e',strtotime('Wed, 26 Oct 2022 16:11:30 -1100'));
?>
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
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
I am getting Google Analytics data via API using google-api-php-client
Everything fine except one thing, I can't convert day timestamp into readable value. The day timestamp looks like 20150724, date('M j', $date); always shows Aug 22 even for different timestamps.
How to fix it?
If you don't tell PHP what the number is, it will assume it's a UNIX timestamp. 20150724 is 22nd August 1970...
So just tell it how it's formatted:
<?php
$google_date = '20150724';
$date = DateTime::createFromFormat('Ymd', $google_date);
echo $date->format('M j');
date() expects the $date to be a timestamp which is the number of seconds since the Unix epoch. Try converting $date with strtotime.