Jump to content

Dates


AV1611

Recommended Posts

I need to create a database with two + columns.  one needs to have the date as "1/1/2007" and the other as "Monday" sequential through all of 2007.

I really just need to figure out how to sequence the php to make all the dates... I can handle the rest... Thanks.

Link to comment
Share on other sites

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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.