0

Start Time, End Date and End Time variables.

The Date Variables are formatted yyyy-mm-dd

The Time Variables are formatted hh-mm (however only on hour numbers are usable, e.g Minutes is always 00)

I can insert these variables into my database no problem, however before I do I want to check that the start date and time is before the end date and time. I know how to check if the time is earlier, and the date is earlier, but I cannot check the time and date together and I would appreciate any help?

$_POST['start_time']
$_POST['end_time']
$_POST['start_date']
$_POST['end_date']

are the variables and how I am grabbing them.

Ryan
  • 71
  • 1
  • 2
  • 9

4 Answers4

7

Use DateTime objects to make life simple for yourself:

<?php
// assuming the following values...
$_POST['start_time'] = '06:00';
$_POST['end_time'] = '10:00';
$_POST['start_date'] = '2012-01-01';
$_POST['end_date'] = '2013-06-02';

//set up two DateTime objects
$start = DateTime::createFromFormat('Y-m-d H-i', $_POST['start_date'] . ' ' . $_POST['start_time']);
$end = DateTime::createFromFormat('Y-m-d H-i', $_POST['end_date'] . ' ' . $_POST['end_time']);

// check if they're valid
if ($start < $end) {
  echo 'We are good...';
} else {
  echo 'Something bad happened...';
}

Bear in mind that this assumes that your $_POSTed values are valid. If you haven't sanitized them already, wrap it in a try/catch at least.

LeonardChallis
  • 7,759
  • 6
  • 45
  • 76
  • The opener told that he has an incoming time format hh-mm. Having this, your code will not work. And that's the original problem and the reason why I've suggested to use `DateTime::createFromFormat()` – hek2mgl Apr 16 '13 at 13:25
  • what if he just add str_replace('-', ':', $_POST['start_time'])? In this way, the solution above is perfect – Federico J. Apr 16 '13 at 13:27
  • Good catch @hek2mgl, cheers. I was thinking about changing to use a str_replace but I feel using `createFromFormat()` is prettier and easier to read. – LeonardChallis Apr 16 '13 at 13:29
  • 1
    Yeah, another reason why `DateTime` simplifies your life ;) – hek2mgl Apr 16 '13 at 13:30
3
function getTime ($ymd, $hi) {
    return strtotime($ymd." ".$hi);
}
if (getTime($_POST['start_date'], $_POST['start_time']) < getTime($_POST['end_date'], $_POST['end_time'])) {
    echo "Ok!";
}

Simply convert it to an Unix-timestamp and then compare.

bwoebi
  • 23,637
  • 5
  • 58
  • 79
  • great thats perfect and simple! I really appreciate it! – Ryan Apr 16 '13 at 13:15
  • (I hope that your format is `hh:mm` instead of `hh-mm`; else you have to do `str_replace("-", ":", $hi)` in the function) – bwoebi Apr 16 '13 at 13:17
  • 2
    as I commented in my answer, I think this is the best answer if you're using old PHP versions with no DateTime objects, but I think the best it's something as @Leonard Challis posted – Federico J. Apr 16 '13 at 13:24
  • Yeah, this should not being used for new code. Use the `DateTime` class instead – hek2mgl Apr 16 '13 at 13:34
2

I would use DateTime::createFromFormat() for it. Like this:

$start = DateTime::createFromFormat('Y-m-d H-i',
             $_POST['start_date'] . ' ' . $_POST['start_time']);
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
0

Try exploding the arrays and then using mktime() function to pass the date to seconds. Then, just compare both dates, the bigger in seconds is the later.

list($strHour, $strMin) = explode('-', $_POST['start_time']);
list($endHour, $endMin) = explode('-', $_POST['end_time']);
list($strYear, $strMonth, $strDay) = explode('-', $_POST['start_date']);
list($endYear, $endMonth, $endDay) = explode('-', $_POST['end_date']);

$startSeconds = mktime($strHour, $strMin, 0, $strMonth, $strDay, $strYear);
$endSeconds   = mktime($endHour, $endMin, 0, $endMonth, $endDay, $endYear);

if ($startSeconds > $endSeconds) {
    echo 'Start is bigger';
}
Federico J.
  • 15,388
  • 6
  • 32
  • 51
  • Can you please give me an example of how I would use this? I have never used mktime() function before – Ryan Apr 16 '13 at 13:10
  • updated to see the example. I've seen the other answers, if you can use objects as DateTime, use it, but check first the possible problems in php manual, as the PHP version you're using, because if you're using an old PHP version, maybe you don't have access to it (I remember I had an old version and I couldn't use it in production). About strtotime, it's a very good answer, but you must be very careful because it may fail if you are not using "the right format": check this answer -> http://stackoverflow.com/questions/6919478/php-problem-with-strtotime – Federico J. Apr 16 '13 at 13:22