Jump to content

Recommended Posts

I wonder how will I able to this in php. I was able to do this in mysql using this

 

i.item_reg >= DATE_ADD(CURDATE(), INTERVAL -3 DAY)

 

it will show only the items that were added 3 days ago up to now.

 

 

In php, I want to echo something out if an item is added 3 days ago up to now

 

I have a column in mysql where it is name item_reg and the format is datetime.

 

I tried this

 

if($item_reg >= strtotime(date("Y-m-d H:i:s"),3))
	{
		//echo something here
	}

 

but I think it's far from what I'm expecting... I tried searching it but I can't find any reference.. Anyone?

Link to comment
https://forums.phpfreaks.com/topic/267542-time-interval-in-php/
Share on other sites

The first argument to strtotime() is what you want to do to the time given as the second argument (which defaults to right now if you don't give one). So

strtotime("-3 days")

to give you three days ago.

 

But since $item_reg is a DATETIME string you have to turn the Unix timestamp that strtotime() returned into a string. That's where date() comes in.

date("Y-m-d H:i:s", strtotime("-3 days"))

The second parameter to strtotime is a time() value, if it is omitted, the current time is used.

 

// $item_reg came from the database & is a database formatted DATETIME: YYYY-MM-DD hh:mm:ss
if (strtotime($item_reg) >= strtotime('-3 days')) {
    // Registered 3 or more days ago

 

Note that this method also considers the TIME part of the Date-Time values.

 

I usually just use strtotime() on datetimes from the (mySql) database. It is a well-known, well-structured value and I have never had a problem with it.

 

Note that if the value is a DATE value from the database, it will not have a TIME component, and strtotime() will apply the time component (hh:mm:ss) from the current system time.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.