I am processing some strings in a PHP script which may be in Y-m-d H:i:s
or Y-m-d
format. I need to remove the H:i:s
portion from the datetime strings so that I am consistently dealing with Y-m-d
date strings.
How can I remove this trailing string (and its leading space) if it exists?
My strings might look like 2003-11-24 23:52:18
or 2009-01-25
.
My expected results from these strings are 2003-11-24
and 2009-01-25
.
I do not need to validate that these strings represent real "date" or "datetime" values.
Since the length of the date is always fix. You can use substr
.
// With 00:00:00
$date = '2003-01-2800:00:00';
echo substr($date, 0, 10) . '<br>';
// Without 00:00:00
$date = '2003-01-28';
echo substr($date, 0, 10) . '<br>';
// With other time part
$date = '2003-01-2812:00:11';
echo substr($date, 0, 10);