php convert date formats [duplicate] - php

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 ));

Related

Convert date format from Y-m-d H:i:s into dmy [duplicate]

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');

Timestamp convert of US date format [duplicate]

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

How to convert String to Date in php? [duplicate]

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'));

PHP change the date to Y-m-d H:i [duplicate]

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);

converting between date formats

how would you convert a date stored as
2011-01-18 11:51:41
into
18-01-2011 11:51:41
using PHP?
many thanks in advance!
date('d-m-Y H:i:s', strtotime('2011-01-18 11:51:41'));
More reliable than using strtotime(), assuming you're on PHP 5.3+
$oldtime = date_parse_from_format('Y-m-d h:i:s', '2011-01-18 11:51:41');
$newtime = date('d-m-Y h:i:s', $time);
However, the date format you're converting FROM suggests it's coming from a MySQL datetime field, in which case you could also do:
SELECT DATE_FORMAT(yourfield, '%d-%m-%Y %H:%i:%s')
and save yourself a full roundtrip in PHP.
Convert the old date to UNIX time with strtotime(), then output it in the new format with date()
$olddate = "2011-01-18 11:51:41";
$newdate = date('d-m-Y H:i:s', strtotime($olddate));
echo $newdate;
// 18-01-2011 11:51:41
$your_date = "2011-01-18 11:51:41";
echo date('d-m-Y H:i:s', strtotime($your_date));
demo

Categories