Jump to content

[SOLVED] Subtracting datetime


Kemik

Recommended Posts

Hello,

 

If I want to display a message saying "Ready" if the $datemade variable is more than 48 hours ago but $datemade is stored as a datetime in the database how would I do it?

 

I can create the code using strtotime("-2 days") and it works to see if the $datemade is more or less than 2 days but I cannot get it to show how many hours are left.

 

Basically,

 

if ($datemade > 48 hours){

  echo "Ready";

} else {

  echo $hours . "hours " . $mins . "mins remaining until ready.";

}

Link to comment
https://forums.phpfreaks.com/topic/65888-solved-subtracting-datetime/
Share on other sites

Solved.

 

After some digging around I found some info and come up with the following:

 

<?php
// $datejoined can be replaced with which ever datetime variable you want. The following code checks if the user joined the site 48 hours ago  (2 days) and if so, shows an image. If not, displays the remaining hours.

$days2 = strtotime("-2 days");
if (strtotime($datejoined) < $days2) {
$eligible = '<img src="'.base_url().'images/icons/yes.png" width="16" height="16" alt="Ready To Play" />';
} else {
$remaining = intval((strtotime($datejoined) - $days2)/3600); 
$eligible = 'In '.round($remaining).' hours';
}
?>

 

Hope this helps someone in the future.

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.