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

Link to comment
Share on other sites

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.