Format jQuery date in PHP - 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));
?>

Related

How to Convert Date Format in PHP

how to convert Fri Jul 19 2019 00:00:00 GMT 0800 (Singapore Standard Time) this format to mysql date format using PHP?
I tried to use strtotime but none of them working for me
date("Y-m-d H:i:s", strtotime("Fri Jul 19 2019 00:00:00 GMT 0800 (Singapore Standard Time)"));
The date fails to parse because it's in a very weird format.
If you can't control the format of the incoming date you could grab the different parts using regex and parse them:
$rawDate = "Fri Jul 19 2019 00:00:00 GMT 0800 (Singapore Standard Time)";
preg_match('/(.*?) GMT (\d+)\s\(.*?\)/', $rawDate, $matches);
$date = DateTime::createFromFormat('D M j Y H:i:s', $matches[1])
->setTimezone(new DateTimeZone('+' . $matches[2]));
echo $date->format('Y-m-d H:i:s'); // outputs 2019-07-19 06:00:00
Example: https://3v4l.org/fbuMk

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: 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