tags:

views:

97

answers:

2

so i have a date format like 07-09-10 and i want to know how to get ago from that date and if i can have a conditional like

if(is_date_with_1_week_of_above_date){ //do something }

A: 

Your date doesn't make clear the format (is it MM-DD-YY, DD-MM-YY, YY-MM-DD, etc.)? But an example using ISO 8601 date format is this:

$oneWeekAgo = strftime("%Y-%m-%d", strtotime("2010-07-09") - 60*60*24*7);

For a comparison, you can use the UNIX timestamp values

$date = "2010-07-09";
$compareDate = "2010-07-03";
$curTimestamp = strtotime($date);
$compareTimestamp = strtotime($compareDate);

if(abs($curTimestamp - $compareTimestamp) < 60*60*24*7)
{
    // within 1 week
}

Edit

Per the comment on the date format, dd-mm-yy is a recognized format for dates, but mm-dd-yy is not in strtotime as seen here:

http://www.php.net/manual/en/datetime.formats.date.php

For it to work, you'd have to convert the dashes to slashes.

Also, if you're looking if the date is specifically one week prior,

$date = str_replace('-','/',"07-10-10");
$compareDate = str_replace('-','/',"07-03-10");
$curDate = strftime("%m/%d/%y", strtotime($date));
$compareDate = strftime("%m/%d/%y", strtotime($compareDate) + 60*60*24*7);

if($curDate == $compareDate)
{
    // is one week prior
    echo "OK";
}
Zurahn
its 07-10-2010 and i want to find 07-03-2010
Matt
Then that would be:$oneWeekAgo = strftime("%m-%d-%Y", strtotime("07-10-2010") - 60*60*24*7);Basically you're converting things to seconds and then it's simple subtraction/addition from there. 60 seconds = 1 minute, 60 minutes = 1 hour, 24 hours = 1 day, 7 days = 1 week. This works for things with definitive lengths, but not so great for lengths that have variations. How would you define 1 "month", as the length of a month can vary?
Klinky
A: 

For checking date with relation to the Current Timestamp

if( strtotime( '-1 week' )>=$dateToCheck ) {
 # $dateToCheck is within the last week
}

The other responses have good solutions for simple checking whether two date/times are within 1 week of each other - no point me repeating them.

Lucanos