Getting date time from a string [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert timestamp to readable date/time PHP
Given 1328520572 as a datetime string,
how can i get the datetime in specific format from it ?
I'd like to use
str = '1328520572';
str_ret=datetime.Parse("M:D:Y H:M:S",str);

$str = '1328520572';
echo date("M:D:Y H:M:S", (int)$str);
But your date format is wrong, check the right format here.

Properly formatted datetime?
$unix = '1328520572';
$datetime = date('Y-m-d H:i:s', $unix);

$str_ret = date("m:d:Y H:i:s", $str);

Try this:
$str = '1328520572';
$str_ret = date('M:D:Y H:M:S', strtotime($str));

Related

How to get date("Y-m-d") from date("Y-m-d H:i:s")? [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
how can I get and store date('2017-02-02') in php variable from result : datetime("2017-02-02 17:02:03").
Do you mean getting 2017-02-02 from 2017-02-02 17:02:03, well you have many options,
$date = date('Y-m-d', strtotime('2017-02-02 17:02:03'));
OR
list($date) = explode(" ", '2017-02-02 17:02:03');
OR
$arr = explode(" ", '2017-02-02 17:02:03');
$date = array_shift($arr);
OR
$dateObj = DateTime::createFromFormat("Y-m-d H:i:s", '2017-02-02 17:02:03');
$date = $dateObj->format("Y-m-d");
You need to convert the date to a timestamp with strtotime()
Example:
<?php
echo date("Y-m-d", strtotime('2017-02-02 17:02:03'));
?>
I hope this will help you!

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

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

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

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

date format change from string [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Strtotime() doesn’t work with dd/mm/YYYY format
I have this variable which i get the info like this:
echo $start=$_REQUEST['to'];
It outputs something like this:
2/04/2012
What i need is to convert it like this: 20120402
Could you please help me? I tried strotime and no success..
I tried converting the string before in a date format, then i converted it in a Ymd format, but i kept receiving a strange date, something like 1970 !
I tried this:
$time = strtotime( $date );
$myDate = date( 'y-m-d', $time );
thanks!
It may work.
<?php
$start=$_REQUEST['to'];
$date = explode("/",$start);
$size = sizeof($date);
for($i=$size;$i>=0;$i--) {
$date_get .= $date[$i];
}
echo $date_get;
?>
You should use strftime instead of date.
$myDate = strftime('%Y%m%d', $time);

Categories