Page 1 of 1

Help with dates

Posted: Wed Nov 02, 2005 8:45 am
by mhouldridge
Hi,

I have the following script which produces a selection box with various dates, ie....

1-11-2005
2-11-2005
3-11-2005

I want to store a specified date in mysql, stripping the hash and changing it to mysql date so that I can do date functions, ie. days between dates.

Any ideas? Here is my script;

Code: Select all

<?
$leap = array(1, 0, 0, 0, 1, 0); //<- Not accurate! Look up the leapyears. 
$years = array(2005, 2006, 2007); //<- List of years 

echo "<SELECT NAME=Date><OPTION>Choose One</OPTION>n"; 

foreach ($years as $year) {  //Cycle thru years 

  for ($i = 1; $i<=12; $i++) { //Cycle thru months 

    $day = 1; //Start at day 1 

     while ($day <= date('t', $month = mktime(0, 0, 0,  $i, 1, $year)))  { //Cycle thru days 
       echo "<OPTION VALUE={$day}, " 
                  .date('m', $month) 
                  .", {$year}>{$day} "
                  .date('m', $month)."-{$year}</OPTION>\n"; 
       $day++; 
    } 
  } 
} 

echo "</SELECT>\n"; 
?>

Posted: Wed Nov 02, 2005 8:50 am
by ambivalent
An option:

Code: Select all

explode("-", $day)

Posted: Wed Nov 02, 2005 9:06 am
by mhouldridge
Ok,

Is there a way to convert and add this numeric to a mysql date?

Posted: Wed Nov 02, 2005 9:10 am
by yum-jelly
MySQL date is yyyy-mm-dd

Code: Select all

<?

$date = '3-11-2005';

$date = preg_replace ( '!([0-9]{1,2})-([0-9]{1,2})-([0-9]{4})!', '\\3-' . sprintf('%02s', '\\2') . '-' . sprintf('%02s', '\\1'), $date );

echo $date;

// then insert $date into your database DATE type field

?>

yj

Posted: Wed Nov 02, 2005 9:36 am
by ambivalent
You could also string it back together in a rearranged format:

Code: Select all

$date = $day['2']."-".$day['1']."-".$day['0'];
or

Code: Select all

$date = date("d-m-Y", (mktime(0,0,0,$day['2'], $day['1'], $day['0']));
//I think...

Posted: Wed Nov 02, 2005 9:47 am
by mhouldridge
Thanks for that,

One more thing - my date currently displays like the following;

2 11-2005

I want it to show 2-11-2005

Ive tried everything i know to get it showing this but dont know where to put the hyphen. Here is my code;

Code: Select all

while ($day <= date('t', $month = mktime(0, 0, 0,  $i, 1, $year)))  { //Cycle thru days 
       echo "<OPTION VALUE={$day}, " 
                  .date('m', $month) 
                  .", {$year}>{$day} " 
                  .date('m', $month)."-{$year}</OPTION>\n"; 
       $day++; 
    } 
  } 
}

Posted: Wed Nov 02, 2005 9:59 am
by pickle
You're doing a lot of work over and over. You can make this a little more efficient:

Code: Select all

$month = mktime(0,0,0,$i,$year);//not doing this on every iteration of the loop
$specific_day = date('t',$month);//ditto

//Cycle thru days 
while ($day <= $specific_day)
{ 
     //only doing this once per loop
     //this allows for a cleaner echo as well
     $month_number = date('m',$month);

     echo <<<OPTION
<option value = '$day-$month_number-$year'>
   $day-month_number-$year
</option>
OPTION;
     
     //I've heard putting the increment '++' in front of the variable is microscopically quicker
     ++$day;
}
If you don't want to adopt my changes, then to answer your original question, you put the dash right after the second {$day} (mine has that addition as well.

Posted: Wed Nov 02, 2005 10:12 am
by mhouldridge
Hi,

that code just gives me loads of errors.

I am interested in the part where you mentioned cutting code down....

Posted: Wed Nov 02, 2005 10:14 am
by mhouldridge
This is my code now,

Code: Select all

while ($day <= date('t', $month = mktime(0, 0, 0,  $i, 1, $year)))  { //Cycle thru days 
       echo "<OPTION VALUE={$day}-, " 
                  .date('m', $month) 
                  .", {$year}>{$day} " 
                  .date('m', $month)."-{$year}</OPTION>\n"; 
       $day++; 
    }

I have added the hyphen after the $day, but it still shows a space as before.

Posted: Wed Nov 02, 2005 10:32 am
by pickle
I said to put the hyphen after the *second* {$day}. You put it after the first.

Regarding the code optimization: You're calling mktime on each iteration of the for loop, but $i and $year aren't changing. Therefore, mktime will give you the same value every iteration. Why not pull mktime out of the loop?

You're also calling date('m',$month) twice on each iteration. Since $month isn't changing, you can pull that out of the loop too and only do it once as opposed to 2* the number of days in the month.

My code from above wasn't tested. This fixes the bugs:

Code: Select all

// These three variable are probably duplicated in your code somewhere else
// I just put them in to ensure my code works
$year = 2005;
$i = 11;
$day = 1;



$month = mktime(0,0,0,$i,1,$year);//not doing this on every iteration of the loop
$specific_day = date('t',$month);//ditto
$month_number = date('m',$month);//ditto

echo '<select>';
//Cycle thru days
while ($day <= $specific_day)
{
     echo <<<OPTION
<option value = '$day-$month_number-$year'>
   $day-$month_number-$year
</option>
OPTION;
     //I've heard putting the increment '++' in front of the variable is microscopically quicker
     ++$day;
}

echo '</select>';

?>

Posted: Wed Nov 02, 2005 10:55 am
by mhouldridge
Ah....

Thanks for that. Very interesting, only problem is that my coding skills are pants right now. Hopefully I will progress....

I have been doing php for some months now, about eight in total, but still having problems getting my head round things.

Posted: Wed Nov 02, 2005 11:01 am
by pickle
No worries - that's what this forum is for ;)