June 29th, 2007, 04:57 PM
How to workout todays date minus 7 days?
I have looked around but just can not seem to find the answer.
I have a date in the format say,
and wish to work out the date 7 days before this
and also one calndar month before todays date to
say if it is 1 january 2007 then it would show 1 dec 2006
as long as the date being the 1st is the same the month number is what needs changing to the previous month.
is there an easy way that this can be done.
thanks in advance for any help given
June 29th, 2007, 05:08 PM
a posix timestamp is the current date and time in seconds. there are 86400 seconds in one day.
Originally Posted by jasonc310771
7 X 86400 .. and there you have it. look into php's date() and mktime() functions.
Bugs that go away by themselves come back by themselves
Beware - your loyalty will not be rewarded
June 29th, 2007, 05:11 PM
Something like this...
$date = '2007-01-04 12:34:31' ;
$oneWeekAgo = strtotime ( '-1 week' , strtotime ( $date ) ) ;
$oneMonthAgo = strtotime ( '-1 month' , strtotime ( $date ) ) ;
echo date ( 'Y-m-j G:i:s' , $oneWeekAgo ) . "<br />\n" ;
echo date ( 'Y-m-j G:i:s' , $oneMonthAgo ) . "<br />\n" ;
Verify and sanitize ALL USER DATA.
And, to steal a quote from jeremy, "Explain your problem instead of asking how to do what you decided was the solution." Chances are someone on the forums will know a better or more efficient way to do what you're trying to accomplish.
Avatar: Stolen by me, shown to me by patrick.
June 29th, 2007, 05:16 PM
$format = 'Y-m-j G:i:s';
$date = date ( $format );
// - 7 days from today
echo date ( $format, strtotime ( '-7 day' . $date ) );
echo '<br />';
// - 1 month from today
echo date ( $format, strtotime ( '-1 month' . $date ) );
June 30th, 2007, 12:28 PM
Just a quick note to say thanks.
Originally Posted by printf
June 15th, 2013, 08:18 AM
search database with Y/m/d format in time frame
This is another method if you need to do a search function for your database from a certain number of days ago including all dates up to today. Just insert the $timelimit variable into your mysql_query
$today = date('Y/m/d');
$numdays = 10; // set number of days to search for
$timelimit = "WHERE date = '".$today."' "; // first instance of the string to search for
for ($x = 1; $x <= $numdays; $x++)
$date = date_create($today); // reset $date vaiable
date_sub($date,date_interval_create_from_date_string($x." days")); // subtract increasing number of days
$daysAgo = date_format($date,"Y/m/d"); // format your date to match how it was saved in database
$timelimit .= " OR date = '".$daysAgo."'"; // each string to search for
June 15th, 2013, 11:16 AM
June 15th, 2013, 11:26 AM
Yes, this is why the $date must be reset each time if you are using a for or while loop...
from php.net: "Note that the add() and sub() methods will modify the value of the object you're calling the method on! This is very untypical for a method that returns a value of its own type. You could misunderstand it that the method would return a new instance with the modified value, but in fact it modifies itself! This is undocumented here. (Only a side note on procedural style mentions it, but it obviously does not apply to object oriented style.)"