0

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:

  • your order will ship today.
  • your order will ship tomorrow.
  • your order will ship monday.

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.

4 Answers 4

3

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.

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

1 Comment

This works great! I think that I was a little too hung up on the if statements... Thanks again!
3
$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.

Comments

1

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().

Comments

1

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
}
?>

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.