Adding days to date field
3 posts by 2 authors in: Forums > CMS Builder
Last Post: August 1, 2009 (RSS)
By eduran582 - July 31, 2009
I have a date field I'd like to add X number of days to so I come up with a new date that I can use in an if, else test. I've tried using several date functions from the php.net site but still can't seem to come up with how this is done. It's probably one of those "can't see the forest for the trees" things, but at this point, I can't even get to the woods [tongue]
I found this function but am not sure how to utilize it:
//Use strtotime() function to convert any type of dates to timestamp
<?php
function addDayToDate($timeStamp, $totalDays=1){
// You can add as many days as you want. mktime will accumulate to the next month / year.
$thePHPDate = getdate($timeStamp);
$thePHPDate['mday'] = $thePHPDate['mday']+$totalDays;
$timeStamp = mktime($thePHPDate['hours'], $thePHPDate['minutes'], $thePHPDate['seconds'], $thePHPDate['mon'], $thePHPDate['mday'], $thePHPDate['year']);
return $timeStamp;
}
?>
Anyone had to "add" days to a date?
TIA!
Eric
I found this function but am not sure how to utilize it:
//Use strtotime() function to convert any type of dates to timestamp
<?php
function addDayToDate($timeStamp, $totalDays=1){
// You can add as many days as you want. mktime will accumulate to the next month / year.
$thePHPDate = getdate($timeStamp);
$thePHPDate['mday'] = $thePHPDate['mday']+$totalDays;
$timeStamp = mktime($thePHPDate['hours'], $thePHPDate['minutes'], $thePHPDate['seconds'], $thePHPDate['mon'], $thePHPDate['mday'], $thePHPDate['year']);
return $timeStamp;
}
?>
Anyone had to "add" days to a date?
TIA!
Eric
Re: [eduran582] Adding days to date field
By Dave - August 1, 2009
Hi Eric,
In both MySQL and in the viewers dates are in this format:
2009-07-05 21:05:38
You can convert them to epoch time (seconds since 1970) like this:
$time = strtotime( $myRecord['myDateField'] );
You can add a day to like this:
$time += (60*60*24);
And then convert it back to text like this (see php.net/date for formatting codes):
echo date("D, M jS, Y g:i:s a", $time);
So that's how you do it in PHP. If you're writing a where query for MySQL you can use some MySQL functions though. Here's some examples:
'where' => "publishDate >= NOW()",
'where' => "createdDate >= (NOW() - INTERVAL 1 DAY) ",
'where' => " '2009-07-05 21:05:38' < (myDateField + INTERVAL 1 DAY) ",
Hope that helps!
In both MySQL and in the viewers dates are in this format:
2009-07-05 21:05:38
You can convert them to epoch time (seconds since 1970) like this:
$time = strtotime( $myRecord['myDateField'] );
You can add a day to like this:
$time += (60*60*24);
And then convert it back to text like this (see php.net/date for formatting codes):
echo date("D, M jS, Y g:i:s a", $time);
So that's how you do it in PHP. If you're writing a where query for MySQL you can use some MySQL functions though. Here's some examples:
'where' => "publishDate >= NOW()",
'where' => "createdDate >= (NOW() - INTERVAL 1 DAY) ",
'where' => " '2009-07-05 21:05:38' < (myDateField + INTERVAL 1 DAY) ",
Hope that helps!
Dave Edis - Senior Developer
interactivetools.com
interactivetools.com
Re: [eduran582] Adding days to date field
By eduran582 - August 1, 2009
Hi Dave,
Once again, right on the money! Just what I needed!
Interactivetools.com has the BEST user support I have ever seen and believe me, I've had my problems with others!
Thanks again for your superior support!
Eric
Once again, right on the money! Just what I needed!
Interactivetools.com has the BEST user support I have ever seen and believe me, I've had my problems with others!
Thanks again for your superior support!
Eric