how can I get and store date('2017-02-02') in php variable from result : datetime("2017-02-02 17:02:03").
Asked
Active
Viewed 8,597 times
-1
-
1Is `datetime` a column in mysql? – Qirel Feb 08 '17 at 11:07
-
yes there is datetime column. – chgav007 Feb 08 '17 at 11:08
-
1Then you can select the date with the mysql function `DATE` (see https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html) - or use `strotime()` in PHP, use `DateTime` objects.. May ways to go about this. – Qirel Feb 08 '17 at 11:09
-
All you need to know is right here: http://php.net/manual/en/function.date.php – PhpDude Feb 08 '17 at 11:26
2 Answers
7
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");

beta-developper
- 1,689
- 1
- 13
- 24
1
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!

node_modules
- 4,790
- 6
- 21
- 37