Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php get date a week ago

Tags:

date

php

I'm trying to create a selector for elements that are within a week old. i get a timestamp like this Sun, 02 Jun 2013 22:05:00 GMT but the selector should not be affected by time

E.G.

<?

$curdate = date( 'D, d M Y H:I:s' );
$olddate = "Sun, 02 Jun 2013 22:05:00 GMT";

if($curdate < $olddate){
    // Date is with in a week
} else {
    // Date is older then a week
}

But in this case, it should not be affected by the time online by the day. But i can't get it working...

like image 464
xvilo Avatar asked Jun 12 '13 14:06

xvilo


People also ask

How to get previous week date in PHP?

Usage: $today=date(); print_r(last_week_range($today)); All the above functions that has been given will return the last week range irrespective of the start day of the week..

How to get last 7 days date in PHP?

echo date('d-m-y:D', mktime(0,0,0,$m,($de-1),$y)); Last seven days date value we can find out by using a for loop.

How can I get last week?

By subtracting 7 days from the current date, we get last week's date.

How can I get last Monday date in PHP?

These will get you the dates of the previous Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Saturday. date ( "Y-m-d" , strtotime ( "previous monday" )); date ( "Y-m-d" , strtotime ( "previous tuesday" ));


2 Answers

PHP's strtotime() function is what your looking for.

For example:

echo date('jS F Y H:i.s', strtotime('-1 week'));

You can feed in a number of different string's into the strtotime() function such as:

strtotime('yesterday');
strtotime('-2 days ago');
strtotime('+5 days');
like image 59
ajtrichards Avatar answered Nov 13 '22 04:11

ajtrichards


You should only create a date out of the compariondate. After that you should create a timestamp of the given date Sun, 02 Jun 2013 22:05:00 GMT, and that should be converted to a datestring with only the date. And then you create another timestamp out of that....

If you know what i mean... This should work:

<?php
    // First create the date

    $date = 'Sun, 02 Jun 2013 22:05:00 GMT';

    // To a timestamp
    $t_date = strtotime($date);

    // Noew remove the seconds: First create a new date, with a timestamp of the give date.
    // After that create a datestring with only the date
    $date = date("jS F Y", $t_date);
    // And create a new timestamp
    $t_date = strtotime($date);

    // One week back: time - 60 seconds * 60 minutes * 24 hours * 7 days * -1 to get backwards
    // And we only create a date of this
    $weekback = date('jS F Y', time() + (60 * 60 * 24 * -7) );
    // Create a timestamp
    $t_weekback = strtotime($weekback);

    // Debug
    echo "Date: $date<br/>Date (UTC): $t_date<br/>";
    echo "Last week: $weekback<br/>Last week (UTC): $t_weekback<br/>";

    if ($t_date <= $t_weekback) {
        //Date is older then a week
        echo "Outside a week: last week($t_date) <= The date($t_weekback)";
    }else{
         //Date is within a week
         echo "Within a week: $t_date > $t_weekback";
    }
?>
like image 23
xvilo Avatar answered Nov 13 '22 06:11

xvilo