The most straight-forward and friendly way of parsing dates is the DateTime extension. It has a static method called createFromFormat
:
$date = '30/01/2013 13:30:06';
$format = 'j/m/Y G:i:s';
$time = DateTime::createFromFormat($format, $date);
echo $time->format('l dS F \'y at H.i.s');
The method takes a custom format and a date string. Because you can define the format yourself it is much easier than parsing it "manually".
In order to adjust the date you can use the add()
, sub()
and modify()
methods:
$time->add(new DateInterval('P3DT5H')); // 3 days and 5 hours
echo $time->format('l dS F \'y at H.i.s');
$time->sub(new DateInterval('P9DT1H')); // 9 days and 1 hours
echo $time->format('l dS F \'y at H.i.s');
$time->modify('-1 year -35 days');
echo $time->format('l dS F \'y at H.i.s');
As you can see the modify()
method is slightly easier to use. The two other methods use the DateInterval
class and an awkward format. It is not difficult (just read the documentation and do as it says), but using actual words (i.e. "-3 days -7 hours") is easier to understand.