John Cardarelli
John Cardarelli

Reputation: 3

Help with validating date/time with PHP if statement

I've been reading my way through the PHP documentation, but I've hit a wall. New to PHP here.

The idea of this script is pretty straightforward: Before 10 a.m. on Mon, Tue & Wed - order ships same day. After 10 a.m. on Wed - order ships Monday. All day Thursday, Friday, Saturday and Sunday - order ships Monday

I am trying to check the day of the week and time of day to tell the user when their order will ship. I found that when $current_time = date("H"); for the whole 10:00 a.m. hour I get an error showing all three options:

I'm sure that this is because of my poorly written if statement.

Then I moved on to using date("H:i");, but to no avail. I know that I'm probably doing something wrong with writing something like this:

else if ($d < 4 && $current_time >= 10:00) {

Source:

<?
    $current_time = date("H:i");
    $d = date("N");

    if ($d > 3) {
        echo('<p>your order will ship monday</p>');
    }
    else
        if ($d < 4 && $current_time >= 10:00) {
            if ($d = 1 && $current_time <= 10:00 || $d = 2 && $current_time <= 10:00) {
                echo('<p>your order will ship today.</p>');
            }

            if ($d = 1 && $current_time >= 10:01 || $d = 2 && $current_time >= 10:01) {
                echo('<p>your order will ship tomorrow.</p>');
            }

            if ($d = 3 && $current_time <= 10:00) {
                echo('<p>your order will ship monday.</p>');
            }
        }
?>

I'm sure this could be slimmed down quite a bit. Any help is greatly appreciated.

Upvotes: 0

Views: 5377

Answers (4)

Jacob
Jacob

Reputation: 8334

You don't have a valid syntax, you can't compare 10:00 in PHP...

An easy alternative is to use:

$current_time = date('Hi')

And then compare like:

$current_time >= 1000

Without colons.

I also think you may have confused yourself trying to be too smart with the logic. That code will be hard to understand and maintain if the rules ever change.

Here's a very straightforward easy-to-read alternative:

<?php
    class ShipDate {
        const TODAY = '<p>your order will ship today</p>';
        const TOMORROW = '<p>your order will ship tomorrow</p>';
        const MONDAY = '<p>your order will ship monday</p>';
    }

    $time = date('Hi');
    switch (date('l')) {
        case 'Monday':
        case 'Tuesday':
            if ($time < 1000)
                echo ShipDate::TODAY;
            else
                echo ShipDate::TOMORROW;
            break;
        case 'Wednesday':
            if ($time < 1000)
                echo ShipDate::TODAY;
            else
                echo ShipDate::MONDAY;
            break;
        default:
            echo ShipDate::MONDAY;
            break;
    }

Whilst the code is longer, it is very obvious what the rules are from looking at the code, no comments needed.

Upvotes: 3

Eva611
Eva611

Reputation: 6194

Comparing 10:00 is not valid syntax.

Try changing current time to

$current_time = date("Hi")

and then drop the ':' and compare

$current_time >= 1000

If you're new to PHP and want to ease into classes etc you could use a simple switch like:

<?php
 $time = date("Hi"); 
 $day = date("N"); //sets a numeric value monday = 1 etc

switch ($day) {
    case 1:
        if ($time < 1000)
            {echo "Your order will ship today";}
        else
            {echo "Your order will ship tomorrow";}
        break;
    case 2:
        if ($time < 1000)
            {echo "Your order will ship today";}
        else
            {echo "Your order will ship tomorrow";}
        break;
    case 3:
        if ($time < 1000)
            {echo "Your order will ship today";}
        else
            {echo "Your order will ship Monday";}
        break;
    default:   
        echo "Your order will ship Monday"; //if none of the above match
}
?>

Upvotes: 1

Gaurav
Gaurav

Reputation: 28755

$current_time >= 10:00 will not work, it will give parse error of :

Use

date("H") >= 10 && date("i") >= 0  // hours and miutes

Similarly for other comparisons.

Upvotes: 3

mlemos
mlemos

Reputation: 1245

You should use the PHP strtotime() function to convert date strings into an integer that you can compare with the current time integer value retrieved with time().

Upvotes: 1

Related Questions