Delivery Date - Date Add-on

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JamieLatham
Forum Newbie
Posts: 2
Joined: Tue Jun 01, 2010 3:35 pm

Delivery Date - Date Add-on

Post by JamieLatham »

Hi People,

I'm a bit stumped on this particular problem.
The Code Below seems to work when it feels like it. Today (tuesday) at 4pm i expected the date to change from Wednesday 02 to Thursday 03, but it went blank.

Basically we offer a Nextday delivery if the customer orders before 4pm, after 4 it will go onto the following day.

Any ideas why some days work and some dont?

Any input would be appreciated!

Code: Select all

<?php
$day_today = date("l"); // Today's Day
$time_today = date("G"); // Today's Time 
$tomorrow = date( "jS", strtotime("+1 day") );  // Today's Day + 1
$tomorrow2 = date( "jS", strtotime("+2 day") ); // Today's Day + 2
$tomorrow3 = date( "jS", strtotime("+3 day") ); // Today's Day + 3
$tomorrow4 = date( "jS", strtotime("+4 day") ); // Today's Day + 4
$tomorrow5 = date( "jS", strtotime("+5 day") ); // Today's Day + 5
?>

<!-- Displays the Next Available Delivery Date -->
<?php
if ($day_today == "Monday" && $time_today < 16 ) { // If Today's Day is equal to Monday and Before 4pm, Shows Tomorrows Day & Date
    echo $day_tomorrow; // Show Tomorrows Day
	echo $tomorrow ; // Show Tomorrows Date
	
	} elseif ($day_today == "Monday" && $time_today > 16 ) { // If Today's Day is equal to Monday and after 4pm, Show Day & Date + 1
    echo "Wednesday "; // Due to Time being after 4pm, Show The Day after Tomorrow
	echo $tomorrow2; // Due to Time being after 4pm, Show The Date after Tomorrow
	
} elseif ($day_today == "Tuesday" && $time_today < 16 ) { // If Today's Day is equal to Tuesday and Before 4pm, Shows Tomorrows Day & Date
    echo "Wednesday "; // Show Tomorrows Day
	echo $tomorrow; // Show Tomorrows Date
	
	} elseif ($day_today == "Tuesday" && $time_today > 16 ) { // If Today's Day is equal to Tuesday and after 4pm, Show Day & Date + 1
    echo "Thursday "; // Due to Time being after 4pm, Show The Day after Tomorrow
	echo $tomorrow2; // Due to Time being after 4pm, Show The Date after Tomorrow
	
} elseif ($day_today == "Wednesday" && $time_today < 16 ) { // If Today's Day is equal to Wednesday and Before 4pm, Shows Tomorrows Day & Date
    echo "Thursday "; // Show Tomorrows Day
	echo $tomorrow; // Show Tomorrows Date
	
	} elseif ($day_today == "Wednesday" && $time_today > 16 ) { // If Today's Day is equal to Wednesday and after 4pm, Show Day & Date + 1
    echo "Friday "; // Due to Time being after 4pm, Show The Day after Tomorrow
	echo $tomorrow2; // Due to Time being after 4pm, Show The Date after Tomorrow
	
} elseif ($day_today == "Thursday" && $time_today < 16 ) { // If Today's Day is equal to Thursday and Before 4pm, Shows Tomorrows Day & Date
    echo "Friday "; // Show Tomorrows Day
	echo $tomorrow; // Show Tomorrows Date
	
	} elseif ($day_today == "Thursday" && $time_today > 16 ) { // If Today's Day is equal to Thursday and after 4pm, Show Day & Date + 1
    echo "Monday "; // Due to Time being after 4pm, Show The Day after Tomorrow
	echo $tomorrow4; // Due to Time being after 4pm, Show The Date after Tomorrow
	
} elseif ($day_today == "Friday" && $time_today < 16 ) { // Due to non-delivery at the weekends, the next availble day is Monday, (Today's Day + 3)
    echo "Monday "; // Due to Day beeing Friday, Show Monday
	echo $tomorrow3; // Due to Day beeing Friday, Show the Date + 3
	
	} elseif ($day_today == "Friday" && $time_today > 16 ) { // Due to non-delivery at the weekends & The time being after 4pm, the next availble day is Tuesday, (Today's Day + 4)
    echo "Tuesday "; // Due to Day beeing Friday & after 4pm, Show Tuesday
	echo $tomorrow4; // Due to Day beeing Friday & after 4pm, Show the Date + 4
	
} elseif ($day_today == "Saturday") { // Due to non-delivery at the weekends, the next availble day is Tuesday, (Today's Day + 3)
    echo "Tuesday "; // Due to Day beeing Saturday, Show Tuesday
	echo $tomorrow3; // Due to Day beeing Saturday, Show the Date + 3 (to show Tuesday)
	
} elseif ($day_today == "Sunday") { // Due to non-delivery at the weekends, the next availble day is Tuesday, (Today's Day + 2)
    echo "Tuesday "; // Due to the Day beeing Sunday, Show Tuesday
	echo $tomorrow2; // Due to the Day beeing Sunday, Show the Date + 2 (to show Tuesday)
}
?> 
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: Delivery Date - Date Add-on

Post by yacahuma »

try this. You need to set your own time zone

Code: Select all

   date_default_timezone_set('Etc/GMT+4');
    list($d,$m,$y,$t,$w) = sscanf(date('d-m-Y H N'),'%d-%d-%d %d %d');
   // echo "$d,$m,$y,$t,$w";

    if ($t>=16)
    {
        switch($w)
        {
            case 1://mon
            case 2://tue
            case 3://wed
              $next =  mktime(0, 0, 0, $m  , $d+2, $y);
              break;
            case 4://thursday
            case 5://friday
              $next =  mktime(0, 0, 0, $m  , $d+4, $y);
              break;
            case 6://sat
              $next =  mktime(0, 0, 0, $m  , $d+3, $y);
              break;
            case 7://sunday
              $next =  mktime(0, 0, 0, $m  , $d+2, $y);
              break;
        }
    }
    else
    {
        switch($w)
        {
            case 1://monday
            case 2://tue
            case 3://wed
            case 4://thursday
              $next =  mktime(0, 0, 0, $m  , $d+1, $y);
              break;
            case 5://friday
              $next =  mktime(0, 0, 0, $m  , $d+3, $y);
              break;
            case 6://sat
              $next =  mktime(0, 0, 0, $m  , $d+3, $y);
              break;
            case 7://sunday
              $next =  mktime(0, 0, 0, $m  , $d+2, $y);
              break;
        }

    }
    echo "NEXT IS = " . date('d-m-Y',$next);
JamieLatham
Forum Newbie
Posts: 2
Joined: Tue Jun 01, 2010 3:35 pm

Re: Delivery Date - Date Add-on

Post by JamieLatham »

Thanks for the Reply,

Unfortunatly that hasnt solved the problem.

I have done some troubleshooting and have narrowed it down to the 16 & 17 hours (4 & 5 o'clock)..
I have also reset the timezone in the php.ini file.

Cant for life of me figure out why just them two?

Any more ideas?

Thanks!
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: Delivery Date - Date Add-on

Post by yacahuma »

can you give me an input time that does not work with the code I gave you?
Post Reply