Date problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
sanju
Forum Commoner
Posts: 65
Joined: Sat Jun 21, 2008 2:15 am
Location: Kochi, India

Date problem

Post by sanju »

I have an event calender in my site for add events on the basis of date. We can add

events with repeat freequency like repeat the same events yearly, monthly, etc. The code

I have used is given below.

<?php

function DateAdd($interval, $number, $date)
{
$date_time_array = getdate($date);
$hours = $date_time_array['hours'];
$minutes = $date_time_array['minutes'];
$seconds = $date_time_array['seconds'];
$month = $date_time_array['mon'];
$day = $date_time_array['mday'];
$year = $date_time_array['year'];

switch ($interval) {

case 'yyyy':
$year+=$number;
break;
case 'q':
$year+=($number*3);
break;
case 'm':
$month+=$number;
break;
case 'y':
case 'd':
case 'w':
$day+=$number;
break;
case 'ww':
$day+=($number*7);
break;
case 'h':
$hours+=$number;
break;
case 'n':
$minutes+=$number;
break;
case 's':
$seconds+=$number;
break;
}
$timestamp= mktime($hours,$minutes,$seconds,$month,$day,$year);

return $timestamp;
}

//code for find repeat years from a selected event start date to either 2099(repeat end

date) or 99 times(event repeat times), whichever comes earlier.

$j = 1;
$select_date = explode('-',$eventStart_date);
while($j < 99 && $select_date[2] < 2099)
{
$temptime =

mktime(0,0,0,$select_date[0],$select_date[1],$select_date[2]);
$temptime = DateAdd('yyyy',1,$temptime);

$newDate = strftime('%m-%d-%Y',$temptime);
$select_date = explode('-',$newDate);

$changeFormt = implode('/',$select_date);
$temp = strtotime($changeFormt);
$finalFormat = date('Y-m-d',$temp);

$arrayToInsert['fieldName_eventId'] = $eventId;
$arrayToInsert['fieldName_RepeatDate'] = $finalFormat;
$insertRepeatId =$objName->queryforInsert('tableName',$arrayToInsert);
$j++;
}
?>

The problem is that suppose if I selected the event start date as 25-11-2008 and event

repeat frequency as yearly then the valus added in the table is like follows(only date

field is mentioned here).

2008-11-25
2009-11-25
2010-11-25
2011-11-25
2012-11-25
2013-11-25
2014-11-25
2015-11-25
2016-11-25
2017-11-25
2018-11-25
2019-11-25
2020-11-25
2021-11-25
2022-11-25
2023-11-25
2024-11-25
2025-11-25
2026-11-25
2027-11-25
2028-11-25
2029-11-25
2030-11-25
2031-11-25
2032-11-25
2033-11-25
2034-11-25
2035-11-25
2036-11-25
2037-11-25 ---> after 2037 the date goes to 1969.....
1969-11-25
1970-11-25


What will be the problem..
Any one pls help me out... in solving this

REgards
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Date problem

Post by aceconcepts »

Read the Change Log in http://uk3.php.net/date

Basically: "before PHP 5.1.0 this range was limited from 01-01-1970 to 19-01-2038 on some systems (e.g. Windows)"
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Date problem

Post by novice4eva »

I went through manual for mktime and it had this to say for year parameter
The number of the year, may be a two or four digit value, with values between 0-69 mapping to 2000-2069 and 70-100 to 1970-2000. On systems where time_t is a 32bit signed integer, as most common today, the valid range for year is somewhere between 1901 and 2038, although this limitation is overcome as of PHP 5.1.0.
So which version of PHP are you using?? Check it and well think about an upgrade :mrgreen:
User avatar
sanju
Forum Commoner
Posts: 65
Joined: Sat Jun 21, 2008 2:15 am
Location: Kochi, India

Re: Date problem

Post by sanju »

Hi all,

Thanks for this valuable information... I was just stucked..
Thank you once again..
Post Reply