This question already has an answer here:
Okay so basically I have two variables one called $day
and another called $time
what I'm trying to do is convert both of these into a unix time-stamp so for example;
$day = 'Monday';
$time = '14:00:00';
So what I'm looking for is a $timestamp
variable that would echo out the next Monday
coming up at 14:00:00
in a unix timestamp format.
I'm guessing the hardest part of this would be the fact that the day is not a specific date more a day of the week meaning it would have to select the next monday
coming up, or the next tuesday...
for example.
Thanks for any help.
</div>
The constructor for the DateTime
class is pretty good at figuring this sort of thing out:
<?php
$day = 'Monday';
$time = '14:00:00';
$date = new DateTime("next $day $time");
echo $date->getTimestamp();
// 1475503200
$datetime = new DateTime();
echo $datetime->format('U');
Solution One:
mktime - Get Unix timestamp for a date
echo mktime(23, 24, 0, 11, 3, 2009);
1257290640
Returns the Unix timestamp corresponding to the arguments given. This timestamp is a long integer containing the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.
Arguments may be left out in order from right to left; any arguments thus omitted will be set to the current value according to the local date and time.
mktime($isAM ? $hrs : ($hrs + 12), $mins, $secs, $m, $d, $y);
To handle AM/PM just add 12 to hours if PM.
Solution Two:
strtotime
Returns a timestamp on success, FALSE otherwise.
echo strtotime('2012-07-25 14:35:08' );
Output:
1343219708