This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I have a Date/time value as such;
2014-01-07T16:19:08Z
I wish to convert it to the format below using PHP
2015-03-21 02:12:01
I know I could use string replace twice over to remove the T and Z characters but doesn't seem right..
use strtotime() with date()
echo date('Y-m-d H:i:s',strtotime('2014-01-07T16:19:08Z'));
The date_format() function returns a date formatted according to the specified format.
$date=date_create("2014-01-07T16:19:08Z");
echo date_format($date,"Y-m-d H:i:s");
One more way is there
echo date('Y-m-d H:i:s',strtotime('2014-01-07T16:19:08Z'));
Update
The date_create() function returns a new DateTime object.
Reference
<?php
echo date('Y-m-d H:i:s',strtotime('2014-01-07T16:19:08Z'));
?>
Related
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I have date format (YYYY-MM-DDThh:mm:ss.u):
2017-05-05T18:20:26.000Z
I need to convert it, so it will be without ms (.u) and to add GMT zone at the end of it (YYYY-MM-DDThh:mm:ss+TZD):
2017-05-05T18:20:26+00:00
How to do that? Should I use format() or date_format()?
Thank You!
You could use DateTime::createFromFormat().
$datestring = "2017-05-05T18:20:26.000Z";
$date = DateTime::createFromFormat("YYYY-MM-DDThh:mm:ss.u", $datestring);
Then use format() on $date as you need it.
DateTime::createFromFormat()
DateTime::format()
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I am taking the input in M-d-Y format from calender but I want to convert it into Y-m-d when it store in the database. What method can I use?
You can try this. strtotime is your friend.
echo date("Y-m-d", strtotime('Dec-02-2014'));
or mysql DATE_FORMAT function, but mysql stores dates in yyyy-mm-dd format.
I think it better to do it in Mysql itself. When we have so many function. Like below:
SELECT STR_TO_DATE('Jan-31-2014', '%b-%d-%Y');
You can use it for insert query like below:
Insert into tableName `date` = STR_TO_DATE(InputVal, '%b-%d-%Y');
For more info:http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date
Try this:
$date="05-25-2014";
print(date("y-m-d",strtotime($date)));
use the date() and strtotime() like:
$var = date('Y-m-d', strtotime($timestring))
Converting string to Date and DateTime
copy from: https://stackoverflow.com/a/6239010/2478144
Make not that there is a difference between using forward slash / and hyphen - in the strtotime function. to quote from php.net
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I have the following datetime 2014-02-05 17:12:48 stored in a php variable named $reportDb["reportDate"].
Now, It is in Y-m-d H:i:s format, I want it in d/m/Y H:i:s format. How can I set or reformat this variable?
I tried with datetime::createformat but It doesn't works.
$formatted = date("d/m/Y H:i:s", strtotime($reportDb["reportDate"]))
If you want another method. Even if i would prefer the one already mentioned.
Output 05/02/2014 17:12:48
PHP 5.3 and older:
$dt = new DateTime('2014-02-05 17:12:48');
echo $dt->format(''d/m/Y H:i:s'');
PHP 5.4+:
$dt = (new DateTime('2014-02-05 17:12:48'))->format(''d/m/Y H:i:s'');
with the variable:
$dt = (new DateTime($reportDb["reportDate"]))->format(''d/m/Y H:i:s'');
If I understand you right, $reportDb["reportDate"] is already a DateTime object.
If so, all you need is ...
$reportDb["reportDate"]->format('d/m/Y H:i:s');
Cheers!
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Covert time format in php
I am getting a start_time field value from mysql data base as 2012-08-14 21:30:00
and i want to convert it in php format like 2012-08-14T09:30 is there any method to do
this in php ?
use strtotime()
$date = strtotime('2012-08-14 21:30:00');
echo date('Y-m-d\Th:i',$date);
see this example.for the required date & time format:
$date=date("Y-m-d H:i:s");//get the current date
$d=strtotime($date);//convert in strtotime
echo $final_date=date("Y-m-d\Th:i:s",$d);//in the first argument of date ,put the format whatever you want,but be sure to convert it in strtotime first.
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 2 years ago.
I am tring to make the google news sitemap by php script from mysql.(all the date save as timestamp in +08:00)
But how to converting date to YYYY-MM-DD hh:mm:ssTZD?
For example 1338048000 => 2012-05-26T09:00:00+08:00
echo date("Y-m-d T h:i:s",'1338048000').'+08:00';//2012-05-26 PDT 09:00:00+08:00
Not the result what I need. And how to? Thanks.
How about this?
echo date("c",'1338048000');
I'd say:
gmdate('Y-m-d\TH:i:s\Z', '1338048000');
The T means something, and needs to be escaped. Or, since PHP5, the ISO8601 date format is natively supported with the c character.
Additionally, using gmdate instead of date removes the need to worry about timezones.
echo date("c", "1338048000").'+08:00';