0

If I have an event which consist of 100 tasks, how to rewrite the below code,so i can show all the dealine of each task into <table>? Thanks!

<?php

require("connect.php");


$deadline = mysql_query("SELECT * FROM events WHERE taskid='1001'");
$row= mysql_fetch_assoc($deadline);
$deadline2 = ($row['deadline']);

//dealine for this task(1001) is 2011-3-10-5-45//

$deadline3 = explode("-", $deadline2);


// countdown function
// parameters: (year, month, day, hour, minute)
countdown($deadline3[0],$deadline3[1],$deadline3[2],$deadline3[3],$deadline3[4]);


function countdown($year, $month, $day, $hour, $minute)
{
  // make a unix timestamp for the given date
  $the_countdown_date = mktime($hour, $minute, 0, $month, $day, $year, -1);

  // get current unix timestamp
  $today = time();

  $difference = $the_countdown_date - $today;
  if ($difference < 0) $difference = 0;

  $days_left = floor($difference/60/60/24);
  $hours_left = floor(($difference - $days_left*60*60*24)/60/60);
  $minutes_left = floor(($difference - $days_left*60*60*24 - $hours_left*60*60)/60);
  
  // OUTPUT
  echo "Today's date ".date("F j, Y, g:i a")."<br/>";
  echo "Countdown date ".date("F j, Y, g:i a",$the_countdown_date)."<br/>";
  echo "Countdown ".$days_left." days ".$hours_left." hours ".$minutes_left." minutes left";
}
?>

//--------------------------
// author: Louai Munajim
// website: www.elouai.com
//
// Note:
// Unix timestamp limitations 
// Date range is from 
// the year 1970 to 2038
//-------------------------

Edited by cliffcc: n/a

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by diafol
0
$output = "\n<table>\n\t<thead>\n\t\t<tr>\n\t\t\t<th>Today</th>\n\t\t\t<th>Countdown Date</th>\n\t\t\t<th>Time Left</th>\n\t\t</tr>\n\t</thead>\n\t<tbody>";
$deadline = mysql_query("SELECT * FROM events");
while($row= mysql_fetch_assoc($deadline)){
  $deadline2 = ($row['deadline']);
  $deadline3 = explode("-", $deadline2);
  $output .= countdown($deadline3[0],$deadline3[1],$deadline3[2],$deadline3[3],$deadline3[4]);
}
$output .= "\n\t</tbody>\n</table>";

In your function:

Instead of:

echo "Today's date ".date("F j, Y, g:i a")."<br/>";
  echo "Countdown date ".date("F j, Y, g:i a",$the_countdown_date)."<br/>";
  echo "Countdown ".$days_left." days ".$hours_left." hours ".$minutes_left." minutes left";

Do:

return "\n\t\t<tr>\n\t\t\t<td>". date("F j, Y, g:i a") . "</td>\n\t\t\t<td>" . date("F j, Y, g:i a",$the_countdown_date) . "</td>\n\t\t\t<td>" . $days_left . " days " . $hours_left . " hours " . $minutes_left . " minutes left</td>\n\t\t</tr>";

Just echo $output wherever it's needed.

I have to say, the function looks far more complicated than it needs to be. Perhaps having your datetime in integer field (MySQL) would be easier.

Edited by diafol: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.