habanero25 Posted October 22, 2007 Share Posted October 22, 2007 Hello, I have written a program that gets the date, and compares it with a "Sale Date". If the current date is before the sale date (November 22nd, 2007), it will print "Sale is on!". If it is after, it will print "Sale is over...". $freestuffdate = "22-11-07"; $today = date("d-m-y"); if ($freestuffdate >= $today){ echo "Sale is on!<br />"; } elseif ($freestuffdate<$today){ echo "Sale is over...<br />"; } I am wondering if there is any way to make a code that prints out how many days are left until the end of the sale. How can I tell PHP to subtract $today from $freestuffdate, so that it tells me how many days are left from the current date and the sale date? Thank you in advance, Habanero Link to comment https://forums.phpfreaks.com/topic/74282-getting-days-remaining/ Share on other sites More sharing options...
Barand Posted October 22, 2007 Share Posted October 22, 2007 <?php $freestuffdate = "22-11-07"; list ($d,$m,$y) = explode('-', $freestuffdate); $freestufftime = mktime(0,0,0,$m,$d,$y); $daystogo = floor(($freestufftime - time()) / 86400); echo $daystogo; ?> Link to comment https://forums.phpfreaks.com/topic/74282-getting-days-remaining/#findComment-375305 Share on other sites More sharing options...
corillo181 Posted October 22, 2007 Share Posted October 22, 2007 this is something i did very quick so try it out. maybe Barand ones is better which ever one you feel more comfortable. this a class <?php class sales{ var $today; var $saledate; function sales($d,$m,$y){ $this->saledate = mktime(0,0,0,$m,$d,$y); $this->today = date("d-m-y"); } function daysLeft(){ $daysUntilSale = date('z',$this->saledate); $currentDay=date('z'); $daysRemain = $daysUntilSale - $currentDay; return $daysRemain; } function saleOver(){ if($this->daysLeft() <=0 ){ return 'this sale is over'; }else{ return 'You have '.$this->daysLeft().' day(s) before this sale is over'; } } } $sale = new sales(21,10,2007); echo $sale->saleOver(); ?> to add differen dates just add the 2 last lines of code $sale2 = new sales(21,12,2007); echo $sale2->saleOver(); $sale3 = new sales(10,11,2007); echo $sale3->saleOver(); and so on if you just want to know the days with out any extra code look at date('z',$source) this will return how many days in the year up to the source date; Link to comment https://forums.phpfreaks.com/topic/74282-getting-days-remaining/#findComment-375310 Share on other sites More sharing options...
clearstatcache Posted October 22, 2007 Share Posted October 22, 2007 $freestuffdate = strtotime("07-10-23"); // date format "Y-m-d" for Nov 11, 2007 $today = strtotime(date("Y-m-d")); if ($freestuffdate >= $today){ echo "Sale is on!\n"; $days_to_go = ($freestuffdate - $today) / 86400; // there are 86400 minutes in 1 day if ($days_to_go < 1) { echo "Today is sale day!\n"; } else { echo "$days_to_go days to go before sale day!\n"; } } elseif ($freestuffdate<$today){ echo "Sale is over...\n"; } Link to comment https://forums.phpfreaks.com/topic/74282-getting-days-remaining/#findComment-375311 Share on other sites More sharing options...
clearstatcache Posted October 22, 2007 Share Posted October 22, 2007 86400 seconds i mean in 1 day... Link to comment https://forums.phpfreaks.com/topic/74282-getting-days-remaining/#findComment-375312 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.