From date with time to date without time 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 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];

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

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!

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

PHP Date String Format [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have a string mm-dd-yyyy which i get from a form, which i want to store it in the database of the data-type DATE (yyyy-mm-dd).
How do i format the string and save it in the database ?
$new_format = date("Y-m-d", strtotime('04-28-2012'));
or
$date = new DateTime('04-28-2012');
$new_format = $date->format('Y-m-d');
or in PHP 5.5+
$new_format = (new DateTime('04-28-2012'))->format('Y-m-d');
Try
$date = DateTime::createFromFormat("m-d-Y", '02-15-2012');
echo $date->format('Y-m-d H:i:s') , "\n";
Output
2012-02-15 23:54:52

Getting date time from a string [duplicate]

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

Categories