This topic is locked

To Display Remaing Time In Days

9/21/2008 11:07:58 AM
PHPRunner General questions
B
bochaka author

Hi

I have created a database which has a field named 'expires', i named this $expireddate i enter the date into this field by selecting it from the click on calender in php runner.

In my event for the view item i want to display "this item expires in XXX DAYS"

I get the current date using date("Y-m-d h:i:s",strtotime("+6 hour")) which i called $mytimestring

If ($mytimeString > $expireddate) {echo "EXPIRED";} ELSE {ECHO "ITEM IS OK";};

and the above works fine, it tells me if the item is ok or has expired,
but what i really need is to display this item will expire in xxx days, any help with this is appreciated as my coding skills are not great at the moment and also am unsure in what format phprunner uses to store the date data inputed from the calender.

T
thesofa 9/21/2008

I am not sure exactly what you want, but there have been 2 recent threads about time and date things

Date Stamp and live clock

and

Displaying time left

I hope these help you find a solution

B
bochaka author 9/21/2008

Hi theSofa

In what format does phprunner store the date, ie. unix time stamp or other. i need to compare this field to the current date, now() and display how many days are left.

T
thesofa 9/21/2008

just use the php functions and you will be fine

B
bochaka author 9/21/2008

Hi

I have found by adding a decimal value (as below) to the time now (ie, $mytimestring) , gives me the current date PLUS a decimal value of part of a year, so I can calculate current date plus say 6 months ($mytimeString + .5 >= $expireddate) and compare it to the expiry date. But I am sure there must be a better way, I would like to be sure that it is correct to the nearest day.

$mytimeString = date("Y-m-d",strtotime("+6 hour"));

$expireddate = $values['ItemtExpires'];

If ($mytimeString + .5 >= $expireddate) {echo "Item will within 6 Months";} ELSE {ECHO "ITEM IS OK";};
Any Ideas

B
bochaka author 9/22/2008

Is there a better way to do this.

Admin 9/22/2008

There is a number of ways to calculate a difference between two dates in PHP code. Here is the article that provides sample code:

http://www.developertutorials.com/tutorial...1018/page1.html
More info: http://www.google.com/search?q=calculate+date+difference+php

B
bochaka author 9/23/2008

Ok, So the first thing I would need to do is use explode to extract the date time data from the database stored by phprunner, then possibly convert it to seconds and do the same with the current time, subtract one from the other and then convert the result back into days.

F
fawad112 9/23/2008

Try this

[codebox]<?php

//Count Down of Days:Hours:Minutes:Seconds

//

//Date/Time counting down too.

$datetime=strtotime( "2008-09-01 11:00" );

//Retrieve the current date/time

$date2=strtotime("NOW");

echo $date2;
if ($datetime < $date2) {

print "The Event is here!! or over with!!
".$date2." is greater than ".$datetime."
";

} else {
//List total time by type

//Seconds

echo "total seconds remaining: ".(($datetime-$date2)). "
";

$holdtotsec=(($datetime-$date2));

//Minutes

echo "total minutes remaining: ".(($datetime-$date2)/60). "
";

$holdtotmin=(($datetime-$date2)/60);

//Hours

echo "total hours remaining: ".(($datetime-$date2)/3600). "
";

$holdtothr=(($datetime-$date2)/3600);

//

//Days - a day is a 24 hour period

//This gives days remaining

$holdtotday=intval(($datetime-$date2)/86400);

echo "total days remaining: ".$holdtotday. "
";

//

//Find hours remaining - get days in hours and sub from tothr

$holdhr=intval($holdtothr-($holdtotday24));

echo "hours remaining: ".($holdhr). "
";

//

//Find minutes remaining - get days and hours in minutes and sub from totmin

$holdmr=intval($holdtotmin-(($holdhr
60)+($holdtotday1440)));

echo "minutes remaining: ".($holdmr). "
";

//

//Find seconds remaining - get days hours minutes in seconds and sub from totsec

$holdsr=intval($holdtotsec-(($holdhr
3600)+($holdmr60)+(86400$holdtotday)));

echo "seconds remaining: ".($holdsr). "
";
}

?>[/codebox]

S
steveh 9/24/2008

Your other option is to do it all at the database level as oracle, microsoft sql and mysql all have datetime funcions, you could then return this just like any other field.