user1481850
user1481850

Reputation: 248

php date convert to seconds

I am storing the date of a post in mysql database in this form y/m/d I want to check if the post is older than 3 days. Is there any way I can convert it to seconds ? and then check like so:

if($mysql_date > $three_days_old){
echo "old post";
}else {
echo "new post";
}

Upvotes: 0

Views: 135

Answers (3)

Atul Rai
Atul Rai

Reputation: 350

You can do following :

$d1 = strtotime(date('Y-m-d', strtotime($mysql_date)));
$d3 = mktime(0, 0, 0, date('m', $d1), date('d', $d1)-3,  date('Y', $d1));

$d2 = strtotime(date('Y-m-d', strtotime('3 days ago')));

if ($d3 > $d2) {
  echo 'old post';
}else {
  echo 'new post';
}

Upvotes: 1

zessx
zessx

Reputation: 68820

Using a mix of date() and strtotime() :

$myDate = date('Y-m-d', strtotime($mysql_date));
$oldDate = date('Y-m-d', strtotime('3 days ago'));

if ($myDate > $oldDate) {
     echo "old post";
} else {
     echo "new post";
}

Upvotes: 2

complex857
complex857

Reputation: 20753

Try, strtotime, that can parse various time string formats.

Upvotes: 2

Related Questions