PHP date and Zend_Date conversion? - php

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

Related

Format jQuery date in PHP

I am passing jQuery date in backend. date is Thu Jun 01 2017 00:00:00 GMT 0530 (India Standard Time).
I want to convert this date in 2017-06-19 this format but in php.
$date_str='Thu Jun 01 2017 00:00:00 GMT 0530 (India Standard Time)';
echo date_format(date_create_from_format('Y-m-d', $date_str), 'd-m-Y');
I have tried this solution but it is not working for me.
You can use this code:
$timezoneDataStartPos = strpos($date_str," GMT");
$formattedString = substr($date_str, 0, $timezoneDataStartPos);
$formattedDate=DateTime::createFromFormat('D M d Y H:i:s', $formattedString, new DateTimeZone('Asia/Kolkata'));
echo $formattedDate->format('Y-m-d');
You need to remove " GMT 0530 (India Standard Time)" and make sure to preserve Timezone as Asia/Kolkata
Try this code:
Note : Remove 0530 (India Standard Time) from your date format and use date format shown below my code.
<?php
$date_str='Thu Jun 01 2017 00:00:00 GMT ';
echo date('Y-m-d',strtotime($date_str));
?>

PHP Date Format

I have this format :
Thu Feb 11 2010 00:00:00 GMT+0100 (CET)
My Question :
How can i format this date, to this kind of format in PHP:
2010:01:01 00:00:00 (Datetime)
$date = new \DateTime('Thu Feb 11 2010 00:00:00 GMT+0100 (CET)');
$formatedDate = $date->format('Y:m:d H:i:s');
See https://3v4l.org/TfR85 and for more information on the datetime object http://php.net/manual/en/book.datetime.php
I must also point out that your initial string doesn't follow any proper format. CET or GMT... there is a big difference
Using strtotime():
$s = strtotime("Thu Feb 11 2010 00:00:00 GMT+0100 (CET)");
$s = date("Y-m-d H:i:s",$s);
echo $s; //2010-02-11 00:00:00

Formatting date and time to get date, month, year each in seperate var

In my json response of twitter API I get time stamp like this
Thu Mar 13 14:24:13 +0000 2014
I tried to format in this way:
$created_at = $thing->created_at;
$date = DateTime::createFromFormat('D M d H:m:s O Y', $created_at);
echo $created_at;
echo $date->format('H:m:s');
Which gives result like this:
Thu Mar 13 14:24:13 +0000 2014
2015:12:13 //formated result. How come 2015?????
Wed Mar 12 14:18:14 +0000 2014
2015:06:12
Tue Jan 21 12:50:17 +0000 2014
2018:02:21
Thu Dec 12 09:29:16 +0000 2013
2015:05:12
Why giving wrong result?
I want to get month, year in seperate variable.
You can simplify the creation of the DateTime by doing this:
$dt = new DateTime('#' . strtotime('Thu Mar 13 14:24:13 +0000 2014'));
This parses the date string to a Unix timestamp, and then creates a DateTime object.
echo $dt->format('Y-m-d H:i:s'); // yields the correct result.
You are using month format character m instead of minutes i, thats why you get "wrong" output.
$dt = new DateTime('Thu Mar 13 14:24:13 +0000 2014');
echo $dt->format('H:i:s');

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

how do i convert timezone string into standard timezone format using php

Hi Mates,
I have a timezone string like:
Mon Nov 05 2012 06:54:06 GMT-0600 (Central America Standard Time)
Need to convert these string as below standard timezone format using php
(GMT-06:00)-Central America
Code:
echo $tz = "Mon Nov 05 2012 06:54:06 GMT-0600 (Central America Standard Time)";
echo $tz2 = substr($tz, 0, 34);
echo date("P", strtotime("$tz2"));
Results:
Mon Nov 05 2012 06:52:37 GMT-0600 (Central Standard Time)
Mon Nov 05 2012 06:52:37 GMT-0600
+00:00
Trying Reference: Date
But not resulting such format. Now need help mates, thanks...
New edit - try this...
$tz = "Mon Nov 05 2012 06:54:06 GMT-0600 (Central America Standard Time)";
$time = explode(" ",$tz);
$timezone = explode("(", str_replace(")","",$tz)); // would be better to use a regex
$new_time = "(".$time[5].")-".$timezone[1];
echo $new_time; // gives (GMT-0600)-Central America Standard Time
Old code before exit.....
$tz = "Mon Nov 05 2012 06:54:06 GMT-0600 (Central America Standard Time)";
$new_time = strtotime($tz);
$new_date = date( '(P)-e', $new_time );
echo $new_date;
(-05:00)-America/New_York
echo date("P", strtotime("$tz2"));
echoes
GMT-0600
The name of the region is not available I'm afraid.

Categories