PHP Convert From UTC to Local Datetime format [duplicate] - php

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
Need a little help.
I'm using PHP.
Here is a example date:
Mon Sep 05 2016 15:30:00 GMT+0800 (China Standard Time)
What i want is display a outpout like this:
2016-09-05 15:30:00
I've tried to convert it using this function:
date('Y-m-d h:i:s', strtotime($time));
But the output is:
1970-01-01 08:00:00
Need some advice
Thanks.

Just use DateTime():
$time = 'Mon Sep 05 2016 15:30:00 GMT+0800';
$date = new DateTime($time);
echo $date->format('Y-m-d H:i:s');

<?php
$timezone = -5; //(GMT -5:00) EST (U.S. & Canada)
echo gmdate("Y-m-j H:i:s", time() + 3600*($timezone+date("I")));
?>
This doc would help more
http://php.net/manual/en/function.gmdate.php

$date=date_create("Mon Sep 05 2016 15:30:00 GMT+0800");
echo date_format($date,"Y/m/d H:i:s");

Related

Format Twilio's date format ( Wed, 21 Oct 2015 19:19:53 +0000 ) into timestamp [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I've been trying to convert Twilio's convoluted time format (Wed, 21 Oct 2015 19:19:53 +0000) into timestamp so I can format it into something more digestible in PHP. I've tried various methods but none works.
Can somebody please shed some light?
Just use DateTime::createFromFormat($format, $timestring), then call getTimestamp() on the object it returns:
$datetime = DateTime::createFromFormat("D, j M Y G:i:s O", "Wed, 21 Oct 2015 19:19:53 +0000");
//or $datetime = DateTime::createFromFormat("(D, j M Y G:i:s O)", "(Wed, 21 Oct 2015 19:19:53 +0000)");
$timestamp = $datetime->getTimestamp();
And actually, it looks like it is DateTime::RFC1123 format, so you can just do:
$datetime = DateTime::createFromFormat(DateTime::RFC1123, "Wed, 21 Oct 2015 19:19:53 +0000");

PHP Change 24 hour datetime using strtotime

I am using strtotime for saving date like strtotime($_POST['end_date']) and this give me format 1441785600 that show very fine in google calendar but when I save string like
Thu Sep 03 2015 14:00:00 (24 hour format)
Data stop displaying in google calendar, I want to convert above sample (24Hours) using strtotime and convert it into something like that 1441785600
You may use something like this which currently doesn't give as you want, but may be useful-
$date = 'Sep 03 2015 14:00:00';
$parsedDate = date_parse_from_format('M d Y H:i:s', $date);
$time_stamp = mktime(
$parsedDate['hour'], $parsedDate['minute'], $parsedDate['second'],
date('n', strtotime($parsedDate['month'])), $parsedDate['day'], $parsedDate['year'],
date('I')
);
You could do:
$dateString = 'Thu Sep 03 2015 14:00:00 (24 hour format)';
echo strtotime(preg_replace('~\w+\s(.+)\s\(.+~','\1',trim($dateString)));

PHP convert date and time from UTC to Europe/London [duplicate]

This question already has answers here:
Convert time and date from one time zone to another in PHP
(6 answers)
Closed 7 years ago.
I have the following date string in the following format:
$received = "Tue, 15 Sep 2015 12:35:03 +0000 (UTC)";
I would like to convert this to the Europe/London timezone as the actual time is supposed to read 13:35:03
Any ideas how this can be done?
Thanks
Set a new Timezone maybe you want to set this dynamically then set the new dateTime from database:
$received = "Tue, 15 Sep 2015 12:35:03 +0000 (UTC)";
$tz = new DateTimeZone('Europe/London');
$date = new DateTime($received);
$date->setTimezone($tz);
echo $date->format('H:i:s');
The output is:
13:35:03
The current/correct way to do this:
$received = "Tue, 15 Sep 2015 12:35:03 +0000 (UTC)";
$date = new DateTime($received);
echo $date->format('c'); // 2015-09-15T12:35:03+00:00
$date->setTimezone(new DateTimeZone('Europe/London'));
echo $date->format('c'); // 2015-09-15T13:35:03+01:00
You build the DateTime object from the string, then change the timezone.
hope this helps you need to create DateTime object then convert the time zone
when you display after you change the time zone then it will display correct time.
$t = new DateTime($received);
date_timezone_set($t, timezone_open('Europe/London'));
echo date_format($t, 'Y-m-d H:i:sP');

DateTime format in PHP [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
how can I format datetime in php from: Tue Dec 09 2014 12:00:00 GMT+0200 (FLE Standard Time) to: 2014-12-09 12:00?
Thank you.
Problem solved. I did it by myself:
$datep = $_POST["datetimepicker"];
$placeholders = array('(FLE', 'Standard', 'Time)');
$vals = array('', '', '');
$str = str_replace($placeholders, $vals, $datep);
$newDate = date('Y/m/d H:i', strtotime($str));
echo $newDate;
One method is to use the datetime class:
$dateTime = new DateTime($yourDateTimeVariable);
$dateTime = $dateTime->format('Y-m-d H:i');
echo $dateTime;
Try this -
$date = "Tue Dec 09 2014 12:00:00 GMT+0200";
$newDate = date('Y-m-d H:i', strtotime($date));
You can use strototime combined with date. Something like
$date = "Tue Dec 09 2014 12:00:00 GMT+0200 (FLE Standard Time)";
$format = date("Y-m-d H:i", strtotime($date));

Format a date for PHP [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I have a date:
Mon, 31 Mar 2014 12:19:10 GMT
How can I convert that to this format:
date('Y-m-d H:i:s')
I have found something like this:
$time = strtotime('10/16/2003');
$newformat = date('Y-m-d',$time);
But how do I convert 'Mar' to '03', so I can use that?
How about using DateTime
$date = new DateTime("Mon, 31 Mar 2014 12:19:10 GMT");
echo $date->format("Y-m-d H:i:s");
No need, PHP's strtotime understands month names.
C:\Users\Niet>php
<?php
var_dump(date('Y-m-d H:i:s',strtotime('Mon, 31 Mar 2014 12:19:10 GMT')));
^Z
string(19) "2014-03-31 12:19:10"
strtotime does the trick here too.
This code:
$date = 'Mon, 31 Mar 2014 12:19:10 GMT';
echo $date;
$time = strtotime('Mon, 31 Mar 2014 12:19:10 GMT');
echo date('Y-m-d H:i:s', $time);
will output this:
Mon, 31 Mar 2014 12:19:10 GMT
2014-03-31 14:19:10
Note that your timezone is important here too. I'm in GMT+2 timezone, so final hour is 14 instead of 12.

Categories