Jump to content

date problem


pouncer

Recommended Posts

For starters, I'd recommend you change the data type in which you store your dates to a DATE type. This will allow you to very easily query against it. DATE types are stored as YYYY-MM-DD and can be used in MySQL calculations as well as easily parsed into PHP using functions such as strtotime(). So, assuming you have a DATE type on your database, something like this would work well. This is coming from the perspective of a calendar script, so you could modify it however you like.

<?php
$year = 2007;
$month = 11; // November or date('n') for current month
$days_in_month = date('t', mktime(0,0,0,$month,1,$year));
$available_days = array();
for ($i = 1; $i <= $days_in_month; $i++) // loop through all the days of the month
{
  $ts = mktime(0,0,0,$month,$i,$year); // timestamp for current day in loop
  $date = date('Y-m-d', $ts); // queryable format
  $sql = mysql_query("SELECT * FROM my_table WHERE '$date' BETWEEN start_date AND end_date");
  if (mysql_num_rows($sql) == 0) // no results, so add it to availabilities
  {
    $available_days[] = $i;
  }
}

echo "Days available: " . implode(', ', $available_days);
?>

 

Hope this helps.

Link to comment
https://forums.phpfreaks.com/topic/78092-date-problem/#findComment-395244
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.