0

I am wondering what would be the best date and time format that php can compare later to other dates?

I am trying to do 3 things. Get the date and time, Add 24 hours to the date and time, and later compare that number to the current date and time to verify t has not been 24 hours.

//GET CURRENT DATE AND TIME
$date = date('m/d/Y h:i:s a', time());
//ADD 24 HOURS
$end_date = $date + 24;

//LATER COMPARE THE CURRENT DATE AND TIME TO MY END_DATE
if (date('m/d/Y h:i:s a', time()) > $end_date){
ehco "YOU ARE PAST YOUR TIME";
}else{
echo "still time left";
}

4 Answers 4

1
// now + 1 day (24h)
$end_date = strtotime("+1 day");


    //LATER COMPARE THE CURRENT DATE AND TIME TO MY END_DATE
    if (strtotime("now") > $end_date){
    ehco "YOU ARE PAST YOUR TIME";
    }else{
    echo "still time left";
    }

you can see the reference strtotime

If you need to print the value in a readable format you can use the function DATE

date("d-m-Y h:i:s",$end_date);

Sign up to request clarification or add additional context in comments.

Comments

1

Why not parse the time in to a timestamp, then just compare. There is a magical function called strtotime()

Comments

1

I would recommend using unix timestamps and you can add 24 hours like so:

$date = time();
$endDate = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y'));
//LATER COMPARE THE CURRENT DATE AND TIME TO MY END_DATE
if ($date > $endDate) {
    ehco "YOU ARE PAST YOUR TIME";
} else {
    echo "still time left";
} 

By using timestamps it's just a matter of comparing two integers to see which one is greater than the other.

Comments

0

If you use ISO standard date time, they will sort alphabetically. E.g.:

date("Y-m-d H:i:s", $timestamp);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.