Format a date for PHP [duplicate] - php

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.

Related

PHP Convert From UTC to Local Datetime format [duplicate]

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

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 Date and Time and timezone

Is there any php function that would convert a date string to its equivalent Asia/Manila time.
I tried setting Asia Manila as my default timezone but no avail.
Please see sample below that needs to be convert to Asia/Manila Time
Sun, 12 Jan 2015 08:27:42 +0000,
Mon, 12 Jan 2015 00:14:04 -0500,
Mon, 12 Jan 2015 05:13:34 +0000 (UTC),
Mon, 12 Jan 2015 08:57:47 +0000 (UTC),
Tue, 13 Jan 2015 01:38:04 +0700 (WIT),
Tue, 13 Jan 2015 00:47:31 +0900 (JST),
Mon, 12 Jan 2015 23:27:26 +0000
your assistance is highy appreciated..
Thanks in advance.
This is the easiest way
date_default_timezone_set('Asia/Manila'); // Set your default TZ to Asia/Manila
// strtotime() will convert all timezones to your default
echo date('Y-m-d H:i:s' , strtotime('Mon, 12 Jan 2015 05:13:34 +0000 (UTC)'));
Try this
$date = new DateTime('2000-01-01', new DateTimeZone('Asia/Manila'));
echo $date->format('Y-m-d H:i:sP') . "\n";
Please try the following
$sdt = '2012-05-15 10:50:00';
$stz = new DateTimeZone('UTC');
$dtz = new DateTimeZone('Asia/Manila');
$dt = new DateTime($sdt, $stz);
$dt->setTimeZone($dtz);
$ddt = $dt->format('Y-m-d H:i:s');
Hope this wil help you.
function convert_timezone($from_tz, $to_tz, $time = '2008-08-03 12:35:23') {
date_default_timezone_set($from_tz);
$datetime = new DateTime($time);
$time_newTZ = new DateTimeZone($to_tz);
$datetime->setTimezone($time_newTZ);
$newDate = $datetime->format('Y-m-d H:i:s');
return $newDate;
}
echo convert_timezone('Asia/Kuala_Lumpur', 'America/Los_Angeles');

PHP get date from string

I'm from mail source I get dates like: Mon, 22 Jul 2013 09:08:50 +0200
I must get from this dates date: 2013-07-22.
I try this
$date = "Mon, 22 Jul 2013 09:08:50 +0200";
date("Y-m-d", strtotime($date))
What I do wrong ?
You need to echo the resulting value. Also, note that the output will be affected by the time zone on the machine running the PHP script.
Use like this
$date = "Mon, 22 Jul 2013 09:08:50 +0200";
echo date("Y-m-d", strtotime($date));

PHP Date - Convert from Wed Sep 10 2013 00:00:00 GMT-0500 (EST)

I have a date that is passed to PHP as such:
$date = mysql_real_escape_string($_POST["Date"]);
The date displays like this - Wed Sep 10 2013 00:00:00 GMT-0500 (EST)
Output needed - 2013-09-10
$dt = new DateTime('Wed Sep 10 2013 00:00:00 GMT-0500 (EST)');
echo $dt->format('Y-m-d');
See it in action
<?php
// $date = mysql_real_escape_string($_POST["Date"]);
// You should not use mysql_real_escape_string here.
$date = 'Wed Sep 10 2013 00:00:00 GMT-0500 (EST)';
echo date('Y-m-d', strtotime($date));
$date = mysql_real_escape_string($_POST["Date"]);
echo date('Y-m-d', strtotime($date));

Categories