How to convert string "290416" in date format in php [duplicate] - php

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

Related

PHP how to convert dd/mm/yy to yyyy-mm-dd [duplicate]

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

PHP: convert date time variable into date [duplicate]

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;

From date with time to date without time in PHP [duplicate]

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

Extract hour information from a date string [duplicate]

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

Convert string date to new format [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
How to convert a string, for instance
$datestring = '28.08.14 10.42';
To a new format like date("YmdHis.u")
Thank you
Use date function. There is no such format as what you have required but the what I think you want to achieve is this.
DEMO
<?php
$datestring = '28.08.14 10.42';
$date = date("Y-m-d H:i:s:u", strtotime($datestring));
echo $date; //outputs 2014-08-28 10:42:00:000000
?>
Try this
$datestring = '28.08.14 10.42';
echo date("YmdHis.u",strtotime($datestring));

Categories