This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I have a function to convert EDT to system time,
function edttogmt($date){
$date = str_replace('$','', $date);
$date = str_replace('EDT','', $date);
$date = date('Y-m-d H:i:s',strtotime($date));
return $date;
}
And my date is like $date = '$07/23/2015 12:38:23 PM EDT';
Is it possible to convert this to my server time.The above function creates wrong
You can exactly describe the input format to receive DateTime object
$date = DateTime::createFromFormat('$m/d/Y h:i:s A e', '$07/23/2015 12:38:23 PM EDT');
echo $date->format('Y-m-d H:i:s'); // 2015-07-23 12:38:23
Related
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Date in a URL dd/mm/yyyy
(1 answer)
Closed 4 years ago.
I have date stamps from external source as per below format
Tue 6/02/2018
However when trying to convert the date to Y-m-d for SQL format the date get skewed, using the below;
date("Y-m-d H:i:s", strtotime('Tue 6/02/2018');
//ends up being Y-d-m
I have set the timezone earlier using
date_default_timezone_set('Australia/Brisbane');
strtotime() cannot parse the format "Tue 6/02/2018" correctly. You have to use DateTime::createFromFormat() to parse from your format.
$time = 'Tue 6/02/2018';
$date = DateTime::createFromFormat("D j/m/Y", $time) ;
echo $date->format("Y-m-d H:i:s") ; // 2018-02-06 07:12:19
You also could use new DateTimeZone() as:
$time = 'Tue 6/02/2018';
$tz = new DateTimeZone('Australia/Brisbane');
$date = DateTime::createFromFormat("D j/m/Y", $time, $tz) ;
echo $date->format("Y-m-d H:i:s") ; // 2018-02-06 17:17:48
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I want to convert date format in php.
I have a variable $date = '05-27-2016 15:30'. I want to convert it into '3:30 PM 27-May-2016'.
I try this one date('H:i A d-M-Y',strtotime($date)) but it gives wrong result.
How do I do that?
Use createFromFormat function
$date = DateTime::createFromFormat('m-d-Y H:i', '05-27-2016 15:30');
echo $date->format('h:i A d-M-Y'); // 3:30 PM 27-May-2016
As i Commented out both the format, You need to change the format before changing final for final format.
If you need 3 at 03, just use g at h.
General: Online Check
$date = '27-05-2016 15:30';
echo date('h:i A d-M-Y', strtotime($date)); //03:30 PM 27-May-2016
Or ,
using Object Oriented: Online Check
$date = '05-27-2016 15:30';
$date = DateTime::createFromFormat('m-d-Y H:i', $date);
echo $date->format('h:i A d-M-Y');
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have code:
$date= "2016/Apr/16";
$time="5:00 PM";
I want to convert it to DB format like 2016-04-16 17:00:00 in php .
I am not getting exact code from anywhere.
This way will solve your problem.
<?php
$date= "2016/Apr/16";
$time="5:00 PM";
$fullDate = str_replace('/','-',$date) . " " . $time;
$newDate = date("Y-m-d H:i:s", strtotime($fullDate));
echo $newDate;
?>
Result
More Examples;
http://www.w3schools.com/php/func_date_strtotime.asp
Convert date format yyyy-mm-dd => dd-mm-yyyy
Normally you would use the following code:
date('Y-m-d H:i:s', strtotime($date.' '.$time));
If it returns the year 1970, then the input date (the one from $date variable) is not formatted according to a standard that can be recognised by php.
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have a string mm-dd-yyyy which i get from a form, which i want to store it in the database of the data-type DATE (yyyy-mm-dd).
How do i format the string and save it in the database ?
$new_format = date("Y-m-d", strtotime('04-28-2012'));
or
$date = new DateTime('04-28-2012');
$new_format = $date->format('Y-m-d');
or in PHP 5.5+
$new_format = (new DateTime('04-28-2012'))->format('Y-m-d');
Try
$date = DateTime::createFromFormat("m-d-Y", '02-15-2012');
echo $date->format('Y-m-d H:i:s') , "\n";
Output
2012-02-15 23:54:52
This question already has answers here:
How do I convert an ISO8601 date to another format in PHP?
(2 answers)
Closed 8 years ago.
There is a string:
$time = "2013-07-10T21:00:00.000";
How to convert it to the format "2013-07-10 21:00:00"?
$time = strtotime("2013-07-10T21:00:00.000");
return strftime("%Y-%m-%d %H:%M:%S", $time);
$date = new \DateTime($time);
$date->format('Y-m-d H:i:s');
$time = "2013-07-10T21:00:00.000";
echo date("Y-i-d H:m:s",strtotime($time));