1

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.

  • get the current date, seek forward to the next monday, add your time and get the unix-timestamp. now go implement that code - if you get any problems, the PHP-documentation for the datetime-functions is very good. – Franz Gleichmann Sep 29 '16 at 16:01

2 Answers2

3

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
iainn
  • 16,826
  • 9
  • 33
  • 40
  • 1
    Thankyou; really appreciate that alot. Solved my original question will mark as answer shortly :) –  Sep 29 '16 at 16:11
0
$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
Naresh Kumar P
  • 4,127
  • 2
  • 16
  • 33