Counting down

Hey,

Trying to add 2 weeks to NOW)(), store it in mysql database and then display how manys days and hours are left untill that date arrives for mutiple records. Whats best method to acheive this?

Thanks

If you have access to the DateTime class in PHP, you can calculate the date and store it something like this:


<?php
date_default_timezone_set('Pacific/Honolulu');
$now = new DateTime();
$twoWeeks = new DateTime();
$twoWeeks->add(new DateInterval('P14D'));

$sql = "INSERT INTO table_name (target_date) VALUES ('" . $twoWeeks->format('Y-m-d H:i:s') . "') WHERE id = 1";
// Code to insert the date/time into whatever place into your DB

?>

Then when you want to display the countdown to a particular entry, you could do something like this:


<?php
$sql = "SELECT target_date FROM table_name WHERE id = 1";
// Code to pull the date/time from your DB

$targetDate = new DateTime($dateFromDB);
$now = new DateTime();



if ($targetDate > $now) {
	$remaining = $now->diff($targetDate);
	echo $remaining->format('%R%a days %R%H hours %R%i Minutes') . " Left\
";
} else if ($targetDate == $now) {
	echo "The target date has arrived!";
} else {
	echo "You missed it ... it has come and gone.";
}
?>

Thanks,

I tried thats concept but get the following error:

Fatal error: Call to undefined method DateTime::diff()

$targetDate = new DateTime('2012-06-29 08:25:36');

Is that the correct format for $targetdate?

Cheers

This is another way I found to acheive same thing… Im not sure which way would be classed as better?



	$inittime=time();
	$expdate=strtotime($enddate);
	$timediff = $expdate - $inittime;
	$days=intval($timediff/86400);
	$remaining=$timediff%86400; $hours=intval($remaining/3600);
	$remaining=$remaining%3600; $mins=intval($remaining/60);
	$secs=$remaining%60;
	
	if($enddate == '0000-00-00 00:00:00') {
		$expires = 'Waiting to start';
	}elseif($expdate > $inittime) {
		$expires = ''.$days.' days<br />'.$hours.' hours<br />'.$mins.' minutes';
	}elseif($expdate < $inittime) {
		$expires = 'expired';
	}


You must not be running a version of PHP that supports that class. Oh well, you can do it the old fashioned way.


<?php
	date_default_timezone_set('Pacific/Honolulu');
	$now = time();
	$twoWeeks = $now + (60 * 60 * 24 * 14); // 60 seconds * 60 minutes * 24 hours * 14 days = two weeks
	$remaining = $twoWeeks - $now;
	
	$days_remaining = floor($remaining / (60 * 60 * 24));
	$hours_remaining = floor(($remaining % (60 * 60 * 24)) / (60*60));
	
	echo "Today is " . date('Y-m-d H:i:s', $now) . "<br />";
	echo "Two weeks from now is " . date('Y-m-d H:i:s', $twoWeeks) . "<br />";
	echo "There are " . $days_remaining . " Days and " . $hours_remaining . " Hours left."
?>

Thanks, altho its PHP5 they are running so I would amadgine it would support unless something is disabled…

That class is >= 5.3 so it’s possible they don’t have it available.

[COLOR=#006400]Thread moved to databases forum

You’re

[/COLOR]
You’re probably better off storing the target date of an “event” in the table and in your select query using DATEDIFF() to get the number of days until the event is due. Have a read of this for the various date and time functions available in MySQL


Thanks guys, I think I have a handle on it now…