Jump to content

Dates


AV1611

Recommended Posts

Figure out Jan 1st 2007's time stamp.  Make a for loop that runs from 0 to the desired number of days.

 

In the for loop, use date() to generate each date string in the format you want.  Take the starting time stamp and add the loop iterator * 86400 and pass that as the time stamp.

 

ie:


$start = mktime(0,0,0,1,1,2007);

for($i=0;$i<365;$i++){
  $ts = $start + ($i * 86400);
  $date = date("n/j/Y", $ts);
  $dow = date("l", $ts);
}

 

you could also use the time stamp directly:


$start = mktime(0,0,0,1,1,2007);
$stop = mktime(0,0,0,1,1,2008);

for($i=$start;$i<$stop;$i+=86400){
  
  $date = date("n/j/Y", $i);
  $dow = date("l", $i);
}

 

not sure which is better optimized, but they should do the same thing.  Just depends on how you would rather choose start/end dates.

Link to comment
https://forums.phpfreaks.com/topic/112925-dates/#findComment-580069
Share on other sites

A date in that format has limited use in a database. You cannot do any greater-than/less-than comparisons and more importantly for what you are doing (some type of calender), you cannot do an ORDER BY that_date_format (you would need to add an auto incrementing index to allow an ORDER BY to output those dates in any correct order.) Also, the day of the week name can be found for any date using a simple DATE_FORMAT() function, so storing the day of week name is redundant information.

Link to comment
https://forums.phpfreaks.com/topic/112925-dates/#findComment-580092
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.