Hello:

I want to create a count down to a date with the following variables
1. number of days (90)
2. current date

now using a specific past date stored in db, I want to create a countdown that basically tells me how many days until the 90 days are up

I have something like this,

<?php

$keyMonth = 5; 
$keyDay = 14; 
$keyYear = 2011; 

$month = date('F'); 
$mon = date('n'); 
$day = date('j'); 
$year = date('Y');
$hours_left = (mktime(0,0,0,$keyMonth,$keyDay,$keyYear) - time())/3600; 
$daysLeft = ceil($hours_left/24);
$z = (string)$daysLeft; 
if ($z > 1) {
print "There are <font size=\"4\" color=\"red\">";
print $z;
print "</font> days left until my 90 days are up</p>";
}
?>

in the above example, I'm counting to a date. What I need is the number of days to a future date based on a specified number of days --say 90 days.

Hope I'm making sense...

Can I get some thoughts on this!

Thanks,
Mossa

Recommended Answers

All 6 Replies

Member Avatar for diafol

I'd use the SQL DATEDIFF function for this. php can be messy.

SELECT field1, field2, DATEDIFF(date_field,DATE(NOW())) AS fieldX FROM table1

That should give you the difference in days (integer) from the two dates. I believe that the database held date needs to be in unix format: 2011-02-15. I may be wrong.

Thanks ardv for the reply and the suggestion. I'm thinking of something a bit different. Let me strenghten my request statement:

my database row would consist of an:

id
customernumber
servicearea
date(unix format)

The number of days is something that I'm going statically add (ie:

$numberofdays='90';

to the statement
I want my sql statement to perform the task of using the date in the db and the $numberofdays to tell me how many days until the 90 days have been reached.

Essentally, my out text would be
" There are 50 days remain until the next service " (service being servicearea column from db)

is this making any sense?

Mossa

I think there are a lot of ways to go about achieving this.

One way store the unixtimestamp of the "expiry date" in the DB. Which is time() + (60*60*24*90).

Then, when you query, you can use expiry date - time() = numbers of seconds left (which you can convert to your preferred format).

Thank you for the thought. I just completed the debugging of one method of achieving that task literally a few minutes before this last post. Below is what is working for me.

<?php 
$yourdatetime = '2011-03-21'; 
$timestamp = strtotime($yourdatetime); 

$startDate=$timestamp;  
$limit=90; 

$z=floor((time() - $startDate)/86400); 
$days=$limit-abs($z); 

if ($z > 0) { 
print "There are <font size=\"4\" color=\"red\">"; 
print $days; 
print "</font> days left until my 90 days are up</p>"; 
} 

?>

Thanks for the suggestions!
Best,
Mossa

Some parts seems redundant to me.

$yourdatetime = '2011-03-21'; 
$timestamp = strtotime($yourdatetime); 
$startDate=$timestamp; // Jan 16, 2011

is equivalent to

$startDate = strtotime('2011-03-21');

and when you floor() it will be abs() already.

Perhaps you are right! It is a bit redundant. I'm taking note of it.

Thanks
Mossa

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.