Jump to content

Recommended Posts

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

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;

$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";

}

 

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.