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...
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..
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.
By subtracting 7 days from the current date, we get last week's date.
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" ));
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');
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";
}
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With