This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
I'm trying to convert a date and time variable into date only in a different format:
$test_date = '01/07/2018 10:00-12:00';
$result = substr($test_date, 0, 10);
$newDate = date("Ymd", strtotime($result));
echo $newDate;
Expected result= 20180701
Actual result= 20180107
Tried all the relative variations I can find on here but keep getting 'Ydm' instead of 'Ymd'.
I'm I missing something obvious here?
Looks like it's being parsed 'wrongly' because it's not explicitly stated what the source format is, using DateTime objects, you can do (not tested):
$result = substr($test_date, 0, 10);
$dateTime = DateTime::createFromFormat('d/m/Y', $result);
$newDate = $dateTime->format('Y-m-d');
echo $newDate;
Related
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 2 years ago.
I'm trying to convert convert dd/mm/yy to yyyy-mm-dd. After i researched several methods and I found this.
$var = '20/01/2021';
$date = str_replace('/', '-', $var);
$show_date = date('Y-m-d', strtotime($date));
It works fine with result 2021-01-20.
However, my input was two digit yy only which is 20/01/21 and the result became 2020-01-21.
With an ambigous date like you have it is better to use the DateTime class as you can set the input format. See the following example from the documentation:
$var = '20/01/21';
$date = DateTime::createFromFormat('d/m/y', $var); // "d/m/y" corresponds to the input format
echo $date->format('Y-m-d'); //outputs 2021-01-20
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have to convert this string date 2016-09-26 00:00:00.000 to the yyyy-mm-dd format without other characters.
Can you help me, please?
You can just use the DateTime class along with the format() method:
$d = new DateTime('2016-09-26 00:00:00.000');
echo $d->format('Y-m-d');
Try this:
$datetime = '2016-09-26 00:00:00.000';
$date = date('Y-m-d', strtotime('2016-09-26 00:00:00.000'));
you will get the only date part in 'yyyy-mm-dd' format.
$test = strtotime('2016-09-26 00:00:00.000');
$test1 = date('Y-m-d h:i', $test);
$array1 = explode(' ', $var);
$date_temp = $array1[0];
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I need to convert string value "290416" which is actually date but not in correct format. I need to change it in date format like 29/04/16.
please help.
If you don't need it as a date but only in date format. Meaning you are not performing any date-functions on it but just displaying it as a date you could use
$str = '290416';
$arr = str_split($str, 2);
$date_string = $implode('/', $arr);
The most robust way will be to use createFromFormat, passing in your format and the string, and they you have a DateTime object and can do many things with it.
define('MY_DATE_INPUT_FORMAT', 'mdy');
define('MY_DATE_OUTPUT_FORMAT', 'm/d/y');
$inputDateString = '042916';
$dateObj = DateTime::createFromFormat(MY_DATE_INPUT_FORMAT, $inputDateString);
$outputString = $dateObj->format(MY_DATE_OUTPUT_FORMAT);
This can also be done procedurally:
$date = date_create_from_format(MY_DATE_INPUT_FORMAT, $inputDateString);
echo date_format($date, MY_DATE_OUTPUT_FORMAT);
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
how to re-format datetime string in php?
(9 answers)
Closed 6 years ago.
I have some strings that will be different dates and times. Each one will be different and I'm looking to format them to my liking.
For Example:
2016-04-15 14:20:00
I would like to be able to take this string and use PHP's Date function to format it to my liking.
For Example:
04/15 2:20pm
So would I take the string and convert it to a timestamp first? If so how do I go about doing that?
You can return a new DateTime object formatted according to the specified format by using this function. Then you can format it as you like.
<?php
$testDate = '15-Feb-2009 11:00:34';
$date = DateTime::createFromFormat('j-M-Y H:i:s', $testDate);
echo $date->format('Y-m-d');
?>
try this ..
<?php
$time = strtotime('2016-04-15 14:20:00');
echo date('m/d g:ia', $time);
?>
Second Way
<?php
$format = 'Y-m-d H:i:s';
$date = DateTime::createFromFormat($format, '2016-04-15 14:20:00');
echo $date->format('m/d g:ia');
?>
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
Is it possible extract hour value from a date string like below
20151211091805 +0000
expected output-
09:18:05 (this should also be string)
Try this:
$t = "20151211091805";
echo date('H:i:s',strtotime($t));
Use PHP's date function.
echo date("H:i:s", strtotime("20151211091805"));
Another way using DateTime class.
$dateTime = new DateTime("20151211091805");
echo $dateTime->format('H:i:s');
Try this..
<?php
$timestamp = 20151211091805;
$date = new DateTime("$ts");
echo date_format($date, 'H:i:s');
?>