Reputation: 6251
I'd like my user to be able to input values like:
4 hours
23 minutes
etc.
strtotime works great for converting these values into seconds, but it adds them to the current time. Is there a way of getting it to return the quantity of time entered in total, rather than from now? Or do I need to do something like this:
$time = strtotime($value) - time();
And just for arguments sake, what would happen if the value of time changes between strtotime evaluating it and time evaluating it?
Upvotes: 0
Views: 3145
Reputation: 91963
You can make sure you're always working with the same time using this pattern:
$start = time();
$time = strtotime($value, $start) - $start;
Or even easier, skip the subtraction by setting the second argument to 0:
$time = strtotime($time, 0);
Upvotes: 0
Reputation: 132011
$time = strtotime($value, 0);
$time = strtotime($value) - time();
And just for arguments sake, what would happen if the value of time changes between strtotime evaluating it and time evaluating it?
Then you will miss some seconds (probably 1), because strtotime($value)
is evaluated first and if the time goes by the result of time()
will be bigger then expected (1 second probably ;))
Upvotes: 5