PHP Date format - php

Tue Oct 26 10:39:39 +0000 2010
How to convert format to 2010-10-26

echo date('Y-m-d', strtotime("Tue Oct 26 10:39:39 +0000 2010"))
// 2010-10-26

echo date('Y-m-d');

Using the DateTime object:
echo date_format(new DateTime('Tue Oct 26 10:39:39 +0000 2010'), 'Y-m-d');
or:
$date = new DateTime('Tue Oct 26 10:39:39 +0000 2010');
echo $date->format('Y-m-d');

Related

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

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.

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

PHP: How to convert date+time in string format to format suitable for sorting data?

I am loading articles from RSS and there is the date value formatted in various as:
January 4, 2013
Fri, 04 Jan 2013 13:18:05 +0000
Fri, 04 Jan 2013 07:33:51 EST
Jan 4, 2013
Fri, 04 Jan 2013 02:27:46 GMT
Is there any uniform way, how to save these values into the database column with datatype DATETIME, TIMESTAMP or TIME, which is the most appropriate for sorting these articles?
You can try like this-
echo date("Y-m-d H:i:s",strtotime("January 4, 2013")); // 2013-01-04 00:00:00
echo date("Y-m-d H:i:s",strtotime("Fri, 04 Jan 2013 13:18:05 +0000")); //2013-01-04 13:18:05
echo date("Y-m-d H:i:s",strtotime("Fri, 04 Jan 2013 07:33:51 EST")); //2013-01-04 12:33:51
echo date("Y-m-d H:i:s",strtotime("Jan 4, 2013")); //2013-01-04 00:00:00
echo date("Y-m-d H:i:s",strtotime("Fri, 04 Jan 2013 02:27:46 GMT")); //2013-01-04 02:27:46
updated datetime format.
Use strtotime function. http://php.net/manual/en/function.strtotime.php
date("Y-m-d H:i:s", strtotime($dateString));

PHP date and Zend_Date conversion?

I've got this date :
$date = 'Mon Feb 07 00:00:00 CST 2011';
But I want $date to be formatted as 02-07-2011 only, using Zend framework or core php also.
<?php
$date = new DateTime('Mon Feb 07 00:00:00 CST 2011');
echo $date->format('m-d-Y');
$date='Mon Feb 07 00:00:00 CST 2011';
echo date('m-d-Y',strtotime($date));
Working example at http://codepad.org/gYfgYqED

Categories