Jump to content

php subtracting days


blazingt

Recommended Posts

I'm trying to make this code efficient. So please give give me some advice.

 

So here's the problem, I'm trying to check if a post is older than 7 days.

the single() function checks if it's a post, and the the_date() is the date the post was created.  the_date() uses the same syntax as the date() function in php. So far I can't create a for loop, I tried and it spit out errors. I also noticed that the $sum gets reseted every time with this code. Any help would be appreciated. Here's the code:

if (is_single()) 
{	
	$oldpost=true;
		$sum = strtotime(date("y-m-d", strtotime("$date")) . " -1 day");
		$newdate=date('y-m-d',$sum);
		if ($newdate == the_date('y-m-d'))
		{
			$oldpost=false;
		}
		$sum = strtotime(date("y-m-d", strtotime("$date")) . " -1 day");
		$newdate=date('y-m-d',$sum);
		if ($newdate == the_date('y-m-d'))
		{
			$oldpost=false;
		}
		$sum = strtotime(date("y-m-d", strtotime("$date")) . " -1 day");
		$newdate=date('y-m-d',$sum);
		if ($newdate == the_date('y-m-d'))
		{
			$oldpost=false;
		}
	}
	if ($oldpost = true)
		{
			 return $data1;
		}
} else 
	{
    	return $data2 ;
	}
}

 

To clear the code up a bit, if $newdate  matches the_date then we can conclude that it's not a new post since the script subtracts 1 day 7 times (I only included it 3 times here so it wouldn't clog up your screen). It will subtract 1 day and check if it matches, it will do this 7 times. If there's no match, then we it's obvious that the post is older than 7 days. I wanted to do a for loop since I think this will be more efficient, but I'm having trouble with it. Thanks for reading, and I hope someone can help me.

Link to comment
https://forums.phpfreaks.com/topic/189138-php-subtracting-days/
Share on other sites

What are you trying to do exactly?  Are you pulling these post dates from a database?  If you just need to know if it is older than 7 days why not just use a less than or equal to statement?

 

//$date is your date from the DB

$newpost = $date <= date('Y-m-d', strtotime('-7 days')) ? false : true;

 

If you are collecting the posts data in an array then just do this in the while loop for each post. 

 

Is this what you are trying to do?

 

Nick

Link to comment
https://forums.phpfreaks.com/topic/189138-php-subtracting-days/#findComment-998713
Share on other sites

Thank you for your reply. I'm not that good with php. I'm trying to check if the post is older than 7 days. The function the_date() is a builtin function that returns the date the post was created. So I would need to compare the_date() with the current day, which is date()

I update the code from the advice you gave me.

if (is_single()) 
{	
	$date = the_date('y-m-d');
	$newpost = $date <= date('y-m-d', strtotime('-7 days')) ? false : true;
	if ($newpost == false) //if post is older than 7 days
	{
		 return $data1 ;
	}
} else 
	{
    	return $data2 ;
	}

 

I tried the above code, but it displays the date when the post was created.

Link to comment
https://forums.phpfreaks.com/topic/189138-php-subtracting-days/#findComment-998777
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.