Shipping Schedule (date and mktime)?

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
subminimal
Forum Newbie
Posts: 6
Joined: Fri Feb 11, 2005 10:33 am
Location: USA

Shipping Schedule (date and mktime)?

Post by subminimal »

I am a bit new to PHP. I need to have a calendar at the top of the page, and below shipping info. The client ships every friday, and in the thier words "These people can be retarded, so everything needs to be spelled out". So under the calendar I need to literally print:

february 1: Your order will be shipped on Friday february 11 (Est. arrival date is: february 12-15)

february 2: Your order will be shipped on Friday february 11 (Est. arrival date is: february 12-15)

And so on.

It needs to be dynamic because the client types this out line by line as of now. I have been Googling and manualing and such, and this is what I have come up with so far:

Code: Select all

<?php
$tomorrow  = mktime(0, 0, 0, date("m") , date("d")+1, date("Y")); 
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
$nextyear = mktime(0, 0, 0, date("m"), date("d"), date("Y")+1);
$today = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
$firstday=01;
$td=date("d");
$nf=strtotime('next friday');
echo "<b>Today is ",date("l M.d",$today),"</b><br /><br />";
echo "<b>Next Friday is ",date("l M.d",$nf),"</b><br />";
echo "<b>Tomorrow = ",date("l M.d",$tomorrow),"</b><br />";
echo "<b>Last Month = ",date("l M.d",$lastmonth),"</b><br />";
echo "<b>Next Year = ",date("l M.d, Y",$nextyear),"</b><br /><br />";
if (date ('l') == 'Friday')
&#123;
   echo "Today is Friday<br />";
&#125;
$time = time ();
for ($i = 0; $i < 7; $i++)
&#123;
   if (date ('l', $time += (60*60*24)) == 'Friday')
   &#123;
       echo "Next Friday is on the ".date ("jS of F", $time), "<br/><br />";
       break;
   &#125;
&#125;  
for ($x = 0; $x <30; $x++)
&#123;
	$today = mktime(0, 0, 0, date("m"), date("d") + $x, date("Y"));
	$newday=$td + $x;
	if($newday < 30)&#123;
echo date("l M.$newday",$today),"<br />";
&#125;
&#125;
?>
My initial concern was to get the days of the week, then calculate when friday is. I think I got that. Obviously I'm a bit of a hack, and I know the code is disgusting... my date loop is wrong (hence why it jumps from Feb 28 to march 29, incrementing by 1) so I need to:

1. Calculate how many days there are in the month
2. Print out each day of the week, with the ship date of the following friday, and estimated arrival date.
3. I deally have this and last months calendars at the top of the page with links to each ship date entry for each day. (Most likely just HTML anchors).

The trick is it must be generated on the fly, and by clicking next/last month regenerate based on that month. This is stumping me also. OK guys, I have done my homework, now theres a mind-block. Any suggestions?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

1. date('t');
2. I posted very recently a possibly working example of this. basically involves date('w') and strtotime()
3. basically, find when the first of that month is (strtotime) roll it back to the Sunday before (if not Sunday), then proceed to fill in all weeks from there on out. Substitute your start of week for Sunday.
subminimal
Forum Newbie
Posts: 6
Joined: Fri Feb 11, 2005 10:33 am
Location: USA

Post by subminimal »

Thank you. 2. Question, where is this post? Searched around a bit but couldnt find the thread?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if you searched for "+date +strtotime" under my name, you would have found the thread ;)

viewtopic.php?t=30419&highlight=date+strtotime
subminimal
Forum Newbie
Posts: 6
Joined: Fri Feb 11, 2005 10:33 am
Location: USA

Post by subminimal »

lol I'm sorry serached for date AND strtotime :oops:
subminimal
Forum Newbie
Posts: 6
Joined: Fri Feb 11, 2005 10:33 am
Location: USA

Post by subminimal »

See right now I'm having a mess of trouble with my loop I think. Whats killing me is conceptually I know what needs to be done, but knowing little of PHP its making me nuts. This is what I have:

Code: Select all

<?php
$totaldays=date('t');
for ($i = 1; $i < $totaldays; $i++)
&#123;
	$today = mktime(0, 0, 0, date("m"), date("d") + $i, date("Y"));
	echo date("l M. $i",$today),"<br />";
&#125;
?>
And my output:

Saturday Feb. 1
Sunday Feb. 2
Monday Feb. 3
Tuesday Feb. 4
Wednesday Feb. 5
Thursday Feb. 6
Friday Feb. 7
Saturday Feb. 8
Sunday Feb. 9
Monday Feb. 10
Tuesday Feb. 11
Wednesday Feb. 12
Thursday Feb. 13
Friday Feb. 14
Saturday Feb. 15
Sunday Feb. 16
Monday Feb. 17
Tuesday Mar. 18
Wednesday Mar. 19
Thursday Mar. 20
Friday Mar. 21
Saturday Mar. 22
Sunday Mar. 23
Monday Mar. 24
Tuesday Mar. 25
Wednesday Mar. 26
Thursday Mar. 27

Which A) starts on the wrong day. B) Seems to inexplicably shift to March? Grrrr.
subminimal
Forum Newbie
Posts: 6
Joined: Fri Feb 11, 2005 10:33 am
Location: USA

Post by subminimal »

Sorry fixed the date I believe...still lost lol:

Code: Select all

<?php
$totaldays=date('t') + 1;
echo date('l');
for ($i = 0; $i < $totaldays; $i++)
&#123;
	$today = mktime(0, 0, 0, date("m"), date("d") + $i, date("Y"));
	echo date("l M. d",$today),"<br />";
&#125;
?>
This is wrong.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

absolutely untested.

Code: Select all

$start = strtotime(date('Y-m-01 00:00:00'));
$days = date('t');

for($x = 0; $x < $days; $x++)
&#123;
  echo date('l M. d', $start + 24 * 60 * 60 * $x) . '<br />';
&#125;
subminimal
Forum Newbie
Posts: 6
Joined: Fri Feb 11, 2005 10:33 am
Location: USA

Post by subminimal »

Excellent! Thankyou. Now this code:

Code: Select all

<?php
$start = strtotime(date('Y-m-01 00:00:00')); 
$days = date('t');
$time = time ();

for($x = 0; $x < $days; $x++) 
&#123; 
  echo date('l M. d', $start + 24 * 60 * 60 * $x) . '<br />';
  if (date ('l', $time += (60*60*24)) == 'Friday')
   &#123;
       echo "Next Friday is on  ".date ("M. d", $time), "<br/><br />";
   &#125;
&#125; 

?>
Outputs:

Code: Select all

Tuesday Feb. 01
Wednesday Feb. 02
Thursday Feb. 03
Friday Feb. 04
Saturday Feb. 05
Sunday Feb. 06
Monday Feb. 07
Next Friday is on Feb. 18

Tuesday Feb. 08
Wednesday Feb. 09
Thursday Feb. 10
Friday Feb. 11
Saturday Feb. 12
Sunday Feb. 13
Monday Feb. 14
Next Friday is on Feb. 25

Tuesday Feb. 15
Wednesday Feb. 16
Thursday Feb. 17
Friday Feb. 18
Saturday Feb. 19
Sunday Feb. 20
Monday Feb. 21
Next Friday is on Mar. 04

Tuesday Feb. 22
Wednesday Feb. 23
Thursday Feb. 24
Friday Feb. 25
Saturday Feb. 26
Sunday Feb. 27
Monday Feb. 28
Next Friday is on Mar. 11
Thankyou again for all your help. I can take it from here. :D
Post Reply