I want to know remaining # of days, weeks, years if any left

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

I want to know remaining # of days, weeks, years if any left

Post by eshban »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi ,

    I have e.g. 15 days ago timestamp. Following function just returns no of days.

       I want to know remaining no of mins, hours, days, weeks, year if any left.

Code: Select all

$next_day_time = time() + (15 * 24 * 60 * 60); //15 is next third day timestamp
	
	echo timeLeft($next_day_time);	


function timeLeft($theTime)
	{
		$now = strtotime("now");
		$timeLeft = $theTime - $now;
		
		if($timeLeft > 0)
		{
		$days = floor($timeLeft/60/60/24);
		$hours = $timeLeft/60/60%24;
		$mins = $timeLeft/60%60;
		$secs = $timeLeft%60;
		
		if($days)
		{
		$theText = $days . " Day(s)";
		if($hours)
		{
			$theText .= ", " .$hours . " Hour(s) ";
		}
		}
		elseif($hours)
		{
			$theText = $hours . " Hour(s)";
		if($mins)
		{
			$theText .= ", " .$mins . " Minute(s) ";
		}
		}
		elseif($mins)
		{
			$theText = $mins . " Minute(s)";
		if($secs)
		{
			$theText .= ", " .$secs . " Second(s) ";
		}
		}
		elseif($secs)
		{
			$theText = $secs . " Second(s)";
		}
		}
		else
		{
			$theText = "The time is already in the past!";
		}
		
		return $theText;
	}

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'm pretty tired of editing your posts.

The code you have will almost always be exactly 15 days ahead.
Post Reply