This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 9 years ago.
What I Require
How can I convert 2013-07-26T10:00:00.000+05:30 to Y-m-d H:i:s format.
I need to get 2013-07-26 10:00:00 from this.
.
What I Tried
$dateTime = DateTime::createFromFormat(DateTime::ISO8601,2013-07-26T10:00:00.000+05:30);
echo $dateTime->format('Y-m-d H:i:s');
AND
$dateTime = DateTime::createFromFormat(DateTime::ISO8601, srtotime(2013-07-26T10:00:00.000+05:30));
echo $dateTime->format('Y-m-d H:i:s');
Note: The date was obtained from google calendar's event start and end date.
Use DateTime
$date = new DateTime('2013-07-26T10:00:00.000+05:30');
echo $date->format('Y-m-d H:i:s');
The second argument for createFromFormat should be the string representing the time, not a timestamp. Try removing strtotime and passing in the string directly.
Alternatively you could do:
echo date('Y-m-d H:i:s', strtotime('2013-07-26T10:00:00.000+05:30'));
Try this code
$datetime="2013-07-26T10:00:00.000+05:30";
$time=strtotime($datetime);
echo date("Y-m-d H:i:s",$time);
Related
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I have date in this format 'm-d-Y' (12-31-2017). I want to convert it into a timestamp. Normally, this works:
$timestamp = strtotime($date);
But if I am not mistaken, that works with 'd/m/Y' and not 'm/d/Y'.
What's the solution here?
You can use DateTime::createFromFormat() and then call getTimestamp() on the Object:
//first create DateTime Object
$datetime = DateTime::createFromFormat('m-d-Y', '12-31-2017');
//get timestamp from DateTime
echo $datetime->getTimestamp();
An another solution is the following:
//CONVERT the time string from the desired format
$dateTime=DateTime::createFromFormat('m/d/Y',$date);
echo "UNIX TIMESTAMP method 1".$date->format("U");
// false
echo "UNIX TIMESTAMP method 2".$date->getTimestamp();
You can change date according your format :
$date = '12/31/2017';
$timestamp = date('Y-m-d', strtotime($date));
$timestamp = date('d-m-Y', strtotime($date));
$timestamp = date('m-d-Y', strtotime($date));
if Timestamp
$timestamp = date('m-d-Y h:i:s', strtotime($date));
Try this code .
<?php
$d=strtotime("12/31/2017");
echo "Created date is " . date("m-d-Y h:i:sa", $d);
?>
Output
for more Information about date time format then read PHP Manual
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I trying to get a date in the d/m/Y H:i format. Here's my code
$dateStr = "28/07/2016 10:00";
echo date('d/m/Y H:i', strtotime($dateStr));
Here's the ouput:
01/01/1970 01:00
I trying to convert to other formats, but the result still the same date. I know this kind of obvious but still can't understand why i'm getting that date as result.
You can use DateTime class and it's createFromFormat method to parse the string to date in required format.
Like this,
$date = DateTime::createFromFormat('d/m/Y H:i', "28/07/2016 10:00");
$date = $date->format('Y-m-d H:i:s');
echo $date;
Use Following code
$dateStr = "28/07/2016 10:00";
$dateStr =str_replace("/","-",$dateStr);
echo date('d/m/Y H:i', strtotime($dateStr));
This code achieves what you want to do:
$dateString = '28/07/2016 10:00';
$date = \DateTime::createFromFormat('d/m/Y H:i', $dateString);
echo($date->format('d/m/Y H:i'));
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 9 years ago.
I'm developing a system that recibe a date format like: yyyy/mm/dd-hh-mm-ss.00 and I need convert it to Y-m-d H:i:s.
What I done was:
$newDate = date("Y-m-d H:i:s", strtotime($values[2]));
where $values[2] it is the date in that format, for instace: 2014/01/01-13-18-23.00. But the result is: 1970-01-01 01:00:00
Can you help me?
Thanks
You can try with strptime like
$format = "%Y-%m-%d %H:%i:%s";
echo strptime($values[2], $format);
Try using DateTime
$date = DateTime::createFromFormat('Y/m/d-H-i-s.00', '2014/01/01-13-18-23.00');
echo $date->format('Y-m-d H:i:s');
See demo here
You should be able to do something like this:
$date = DateTime::createFromFormat("Y/M/d-H-i-s.00", $values[2]);
echo $date->format("Y-m-d H:i:s");
The dateTime format for strtotime() function are predefined and you have to follow the rules to get desired result.
Mixed date time formats are available here PHP: Compound Formats - Manual
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Getting time and date from timestamp with php
I am trying to convert this date format:
09/14/2012 10:11
into this other standard timestamp format:
2012-09-14 09:44:54
What is the most efficient way of doing it?
You can use PHP's date and strtotime functions:
$date = '09/14/2012 10:11';
$formatted = date('Y-m-d H:i:s', strtotime($date));
You can use DateTime object
$date = DateTime($myDate);
or your custom input format with createFromFormat
$date = DateTime::createFromFormat('d/m/y H:i:s', $myDate);
format Will make your output
$result = $date->format('Y-m-d H:i:s');
date('Y-m-d H:i:s', strtotime('09/14/2012 10:11'));
Though it's going to be hard to change the time.
Convert first into timestamp with strtotime and convert timestamp into date with date.
$outputDate = date( 'Y-m-d H:i:s', strtotime( $inputDate ));