Question

I've been trying to create a function that converts something like 3d4h into the current unixtime+the number of seconds within that string. I unfortunately have not been successful, and now I'm turning to you all. Thanks for any help!

Was it helpful?

Solution

DateTime() and DateInterval() already do this for you:

$interval = new DateInterval('P3DT4H');
$now = new DateTime();
$now->add($interval);
echo $now->format('Y-m-d H:i:s');

This example adds an interval to "now". You can, of course, make the interval relative to any date and time.

See the interval spec for what valid values are for DateInterval().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top