Jump to content

php to query dates


dadamssg87

Recommended Posts

I'm trying to convert from making potentially 31 queries to pull events to display in each day in a calendar to making one query to pull all the events for that month and putting those events in a multidimensional array to display in the calendar. I'm having trouble figuring out the best way to check if the current day the script is on has events. The events have a start timestamp and an end timestamp and i know i need to compare these values to the start (2011-06-02 00:00:00) and end (2011-06-02 23:59:59) timestamps of the day the script is on.


<?php
$month_number = 6;
$year = 2011;
$day_number = 30;

//cycle through all the days of the month
for($day = 1; $day <= $day_number; $day++)
   {
$start_current_date = mktime(0,0,0, $month_number, $day, $year); //get the very beginning of the day
$end_current_date = mktime(23,59,59, $month_number, $day, $year); //get the very end of the day


//cycle through events/bookings
foreach($bookings as $key => $value)
{

	$startstamp = strtotime($bookings[$key]['start']);
	$endstamp = strtotime($bookings[$key]['end']);

	// part i need help with, most efficient way to determine if script should display events
                // based on $startstamp , $endstamp, $start_current_date, and $end_current_date.

                

}
}
?>

 

any advice would be greatly appreciated!

 

Link to comment
https://forums.phpfreaks.com/topic/238275-php-to-query-dates/
Share on other sites

fiddled with and figured it out.

<?php
$month_number = 6;
$year = 2011;
$day_number = 30;

//cycle through all the days of the month
for($day = 1; $day <= $day_number; $day++)
   {
$start_current_date = mktime(0,0,0, $month_number, $day, $year); //get the very beginning of the day
$end_current_date = mktime(23,59,59, $month_number, $day, $year); //get the very end of the day


//cycle through events/bookings
foreach($bookings as $key => $value)
{

	$startstamp = strtotime($bookings[$key]['start']);
	$endstamp = strtotime($bookings[$key]['end']);

        if($startstamp <= $current_date && $endstamp >= $current_date || $startstamp >= $current_date && $endstamp <= $end_current_date)
	{
                     echo $bookings[$key]['name'];
                }         

}
}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/238275-php-to-query-dates/#findComment-1224478
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.