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
Related
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
I have a date time in string with format Y-m-d H:i:s like this:
$dateTime = '2018-07-06 18:53:21';
i want to conver it into dmy format, like this:
$convertedDateTime = $this->convertDateTime($dateTime);
echo $convertedDateTime;
and the result i expected from above echo is 060718, how can i achieve this?
You can use date_format like this:
echo date_format($date,"Y/m/d H:i:s"); //e.g. 2013/03/15 00:00:00
echo date_format($date,"dmy"); //e.g. 150313 -- Y capital would return 2013
there's two methods to use:
date - https://secure.php.net/manual/en/function.date.php
usage example:
$myDate = '2018-07-06 09:49:00';
$myDate = date('d-m-y', strtotime($myDate));
but more often than not you'll want to use DateTime https://secure.php.net/manual/en/class.datetime.php:
$myDate = DateTime::createFromFormat('Y-m-d H:i:s', '2018-07-06 09:49:00');
$$newDate = $myDate->format('d-m-Y');
\DateTime::createFromFormat('Y-m-d H:i:s', '2018-07-06 18:53:21')->format('dmy');
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 7 years ago.
I have an string date like this: 2015/12/20 13:58:59
I try to convert timestamp:
$idate = $user->multiexplode(array("/"," ",":"),strip_tags("2015/12/20 13:58:59"));
//mktime( $hour , $minute , $second , $month , $day , $year , $is_dst );
$timestamp = mktime($idate[3],$idate[4],$idate[5],$idate[1],$idate[2],$idate[0]);
And now I try to convert real date:
echo 'new date: '.jdate('Y/n/j H:i:s',$timestamp);
Ok...it works but there is a problem!
according of time server,I get variable time.
for examle for location +1 GMT: 2015/12/20 14:58:59
for -1 GMT: 2015/12/20 11:58:59
I want for all server print 2015/12/20 13:58:59 again
You can use DateTime class and it's methods to convert the date and time to unix timestamp. And use strftime() to convert the unix timestamp back to your desired format, like this:
$datetime = "2015/12/20 13:58:59";
// covert timestamp
$unixdatetime = DateTime::createFromFormat('Y/m/d H:i:s', $datetime)->getTimestamp();
echo $unixdatetime . "<br />"; // 1450616339
// now format the unix timestamp
$formatted_datetime = strftime("%Y/%m/%d, %H:%M:%S",$unixdatetime);
echo $formatted_datetime; // 2015/12/20, 13:58:59
Output:
1450616339
2015/12/20, 13:58:59
Here are the references:
DateTime class
strftime()
Use this code.
<?php
$date = '2015/12/20 13:58:59';
//convert to timestamp
$timestamp = strtotime($date);
echo $timestamp;
//convert timestamp back to datetime
$date_again = date('Y/m/d H:i:s', $timestamp);
echo $date_again;
?>
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);
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 ));