Page 1 of 1

PHP code to store Future dates?

Posted: Mon Jul 28, 2008 7:25 pm
by allan.registos
How can we implement scheduling in PHP? I need to store future dates this way:

Every last day of the month
and every 15th day of the month

These values will be stored in a mysql table...

Any help?

Re: PHP code to store Future dates?

Posted: Mon Jul 28, 2008 7:56 pm
by Christopher
If you search these forums for "recurring event" or "recurring date" you will find a number of threads discussing how to implement this.

Re: PHP code to store Future dates?

Posted: Tue Jul 29, 2008 8:01 pm
by allan.registos
arborint wrote:If you search these forums for "recurring event" or "recurring date" you will find a number of threads discussing how to implement this.
It doesn't help. All the examples given are very complicated to use, at least as what I saw being a PHP newbie. Anyway I will try to code it on my own with I think, a simple and as few lines of code as possible, I think this is what PL should be designed for. The examples are just too bloated for me to understand. I don't understand why dates are very complicated to code nowadays.

Thank you very much..
Allan

Re: PHP code to store Future dates?

Posted: Wed Jul 30, 2008 6:35 pm
by Luke
All the examples given are very complicated to use
Of course they are all complicated. You're dealing with a complicated subject. There is no magical code that will make recurring events (or even dates and times for that matter) easy. I've probably started half the threads on this forum pertaining to recurring events and I still have not been able to implement them in anything more than a rudimentary form.

Times and dates are difficult because they aren't really based on a decimal system like most numbers are. You've got 60 seconds per minute, 60 minutes per hour, 24 hours per day (12 of them AM and 12 of them PM), 356 days per year (except on leap year) as well as daylight savings, multiple calendar systems, time zones, and we haven't even gotten to recurring events yet. Let's think about how many ways an event can recur, and this list is by no means exhaustive (it's off the top of my head).

daily
weekly - by day of the week
monthly - by day of the month, by day of the week (every third sunday), things like "last day of the month" or "first day of the month"
yearly - many options here: "3rd sunday of 4th month", the 12th day of the 6th month, etc.

And then you have to consider exceptions such as "except for weekends", "except for holidays", "except on tuesdays", etc. You also have to consider how long this event might recur. Will it recur forever? Until a certain date? After a certain amount of these events have occurred?

Even considering all of this, you still won't be able to schedule something as seemingly simple as Easter.

http://en.wikipedia.org/wiki/Easter
Wikipedia wrote:The calculations for the date of Easter are somewhat complicated. In the Western Church, Easter has not fallen on the earliest of the 35 possible dates, March 22, since 1818, and will not do so again until 2285. It fell on March 23 in 2008, but will not do so again until 2160. Easter last fell on the latest possible date, April 25, in 1943 and will next fall on that date in 2038. However, it will fall on April 24, just one day before this latest possible date, in 2011.

The cycle of Easter dates repeats after exactly 5,700,000 years, with April 19 being the most common date, happening 220,400 times or 3.9%, compared to the median for all dates of 189,525 times or 3.3%.
You'll find that on this forum, if you're willing to put in the time to properly research, we'll be willing to put in the time to help you to the best of our ability, but if you show no effort, neither will we. Get reading!

The first thing you ought to read about is the most well-established standard for calendar data / recurrent events: iCalendar

My company was interested (and still is, but to a lesser degree) in writing a php-based calendar application comparable to (but probably not nearly as good as) Google Calendar. Before this could even be considered, we would need to write a PHP-based library capable of reading and writing iCalendar files so that data could be transferred easily between our application and other scheduling software such as Outlook or Mozilla Firebird (both of which use the iCalendar format to exchange this information).

Also for speed's sake we'd probably need to figure out a way to store these recurring events in a database. You'll find the fruit of all my research in the following threads:

viewtopic.php?f=19&t=65648 - unfinished icalendar library "qCal"
viewtopic.php?t=66248&postdays=0&postorder=asc&start=0 - discussion on recurring event storage (in a database)
viewtopic.php?f=2&t=65534 - more discussion on how to store the events
viewtopic.php?f=1&t=75294 - discussion on unfinished calendar application
viewtopic.php?f=6&t=66039 - discussion on recurring events "terms" - fairly useless LOL

I eventually found a really good solution to recurring event storage / lookup within a database. There was this really nice guy who's website I stumbled upon somehow and he explained to me how he implemented it in postgres, but I use mysql. I never got a chance to properly port his method over. If I can dig up the website I'll let you know. Happy studying!

Re: PHP code to store Future dates?

Posted: Thu Jul 31, 2008 8:23 pm
by allan.registos
The Ninja Space Goat wrote:
All the examples given are very complicated to use
Of course they are all complicated. You're dealing with a complicated subject. There is no magical code that will make recurring events (or even dates and times for that matter) easy. I've probably started half the threads on this forum pertaining to recurring events and I still have not been able to implement them in anything more than a rudimentary form.

Times and dates are difficult because they aren't really based on a decimal system like most numbers are. You've got 60 seconds per minute, 60 minutes per hour, 24 hours per day (12 of them AM and 12 of them PM), 356 days per year (except on leap year) as well as daylight savings, multiple calendar systems, time zones, and we haven't even gotten to recurring events yet. Let's think about how many ways an event can recur, and this list is by no means exhaustive (it's off the top of my head).
Complicated indeed. I think I am looking earlier for a method of finding and storing the 15th and every last day of every month until the scheduled end of date was reach, therefore, computations for seconds, minutes and hours should be eliminated as they are not necessary.

I've found this code somewhere, from a PHP scripts site:

Code: Select all

 
//Getting the From Date
$from = $_POST['start'];
//Getting the To Date
$to = $_POST['stop'];
$rate = $_POST['rate'];
// create values for each date:
$startTime = strtotime($from);
$endTime = strtotime($to);
$values = array();
for($time = $startTime; $time <= $endTime; $time = strtotime('+1 day', $time))
{
   $thisDate = date('Y-m-d', $time);
   $values[] = "('$thisDate', '$rate')";
}
// build the actual query:
$query = sprintf(
   "INSERT INTO test (date, rate) VALUES\n%s",
   implode(",\n", $values)
);
 
I need to modify the " strtotime('+1 day', " part - to find the 15th and last day of every month until the condition at $endTime was satisfied store them in array $values so that I can use it for the actual sql data storage.

With many thanks,
Allan

Re: PHP code to store Future dates?

Posted: Fri Aug 01, 2008 3:22 am
by onion2k
I wouldn't use strtotime(). It's a great function, but in this case I'd use mktime() instead.

Re: PHP code to store Future dates?

Posted: Fri Aug 01, 2008 3:39 am
by phpwalker

Code: Select all

 
<?php
 
$myMonth = 8;
$myYear = 2008;
 
$d_daysinmonth = date('t', mktime(0,0,0,$myMonth,1,$myYear));     // how many days in month
$d_year = date('Y', mktime(0,0,0,$myMonth,1,$myYear));        // year
$d_isleapyear = date('L', mktime(0,0,0,$myMonth,1,$myYear));    // is YYYY a leapyear?
$d_dayofyear = date('d', mktime(0,0,0,$myMonth,1,$myYear));
$d_weekofyear = date('W', mktime(0,0,0,$myMonth,1,$myYear));
 
$d_firstdow = date('w', mktime(0,0,0,$myMonth,'1',$myYear));     // FIRST falls on what day of week (0-6)
$d_firstname = date('l', mktime(0,0,0,$myMonth,'1',$myYear));     // FIRST falls on what day of week Full Name
 
$d_month = date('n', mktime(0,0,0,$myMonth,28,$myYear));         // month of year (1-12)
$d_monthname = date('F', mktime(0,0,0,$myMonth,28,$myYear));         // Month Long name (July)
$d_month_previous = date('n', mktime(0,0,0,($myMonth-1),28,$myYear));         // PREVIOUS month of year (1-12)
$d_monthname_previous = date('F', mktime(0,0,0,($myMonth-1),28,$myYear));     // PREVIOUS Month Long name (July)
$d_month_next = date('n', mktime(0,0,0,($myMonth+1),28,$myYear));         // NEXT month of year (1-12)
$d_monthname_next = date('F', mktime(0,0,0,($myMonth+1),28,$myYear));         // NEXT Month Long name (July)
$d_year_previous = date('Y', mktime(0,0,0,$myMonth,28,($myYear-1)));        // PREVIOUS year
$d_year_next = date('Y', mktime(0,0,0,$myMonth,28,($myYear+1)));        // NEXT year
 
$d_weeksleft = (52 - $d_weekofyear);                     // how many weeks left in year
$d_daysinyear = $d_isleapyear ? 366 : 365;                // set correct days in year for leap years
$d_daysleft = ($d_daysinyear - $d_dayofyear);                // how many days left in year
 
$tomorrow  = mktime(0, 0, 0, date("m"), date("d")+1, date("Y"));
echo date('d', $tomorrow) . '<br />';
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
echo date('m', $lastmonth) . '<br />';
$nextyear  = mktime(0, 0, 0, date("m"), date("d"), date("Y")+1);
echo date('Y', $nextyear) . '<br />';
 
echo $d_dayofyear; echo '<br />';
echo $d_weekofyear; echo '<br />';
echo $d_daysinmonth; echo '<br />';
echo $d_year; echo '<br />';
echo $d_isleapyear; echo '<br />';
echo $d_firstdow; echo '<br />';
echo $d_firstname; echo '<br />';
echo $d_month; echo '<br />';
echo $d_monthname; echo '<br />';
echo $d_month_previous; echo '<br />';
echo $d_monthname_previous; echo '<br />';
echo $d_month_next; echo '<br />';
echo $d_monthname_next; echo '<br />';
echo $d_year_previous; echo '<br />';
echo $d_year_next; echo '<br />';
echo $d_weeksleft; echo '<br />';
echo $d_daysinyear; echo '<br />';
echo $d_daysleft; echo '<br />';
?>
 
yeah, just now was testing the make time function also...got it from php manual, and add something on it...