如何创建这个脚本,进入PHP函数?

时间:2010-11-08 13:42:53

标签: php mysql

以下是来源:https://docs.google.com/document/pub?id=1VxiZbysGLVx71ni0biGm2NbhhSCkPZmGWng-ik7IHNQ

的链接
<?php  
$user1 = mysql_query("SELECT * FROM `users` WHERE `id`='$user->id'");
while($user = mysql_fetch_object($user1)){
   $data2 = mysql_query("SELECT *,UNIX_TIMESTAMP(`start`) AS `start`,0 FROM `users` WHERE `id`='$user->id'");
   $data1 = mysql_fetch_object($data2);
   $date = $user->howlong;
   $timedif1 = $data1->start-3600+$date-time();
   if($data1->start + $date > time()){
       list($h,$min,$sec)=explode(":",date("H:i:s",$timedif1));
       $countdown = $data1->start+$date-time();
   }
}
?>

你有<?php echo $countdown ?>时间结束!

问题:如何在PHP函数中创建此脚本?

2 个答案:

答案 0 :(得分:2)

这将是这样的:

<?php  

// getCountdown() function
function getCountdown($userId)
{
$user1 = mysql_query("SELECT * FROM `users` WHERE `id`='$userId'");
while($user = mysql_fetch_object($user1)){
   $data2 = mysql_query("SELECT *,UNIX_TIMESTAMP(`start`) AS `start`,0 FROM `users` WHERE `id`='$userId'");
   $data1 = mysql_fetch_object($data2);
   $date = $user->howlong;
   $timedif1 = $data1->start-3600+$date-time();
   if($data1->start + $date > time()){
       list($h,$min,$sec)=explode(":",date("H:i:s",$timedif1));
       $countdown = $data1->start+$date-time();
   }
 return $countdown;
}

// call function and pass user id parameter
$countdown = getCountdown($user->id);
// display function output
echo $countdown;

?>

答案 1 :(得分:0)

function yourfuction(){
  $user1 = mysql_query("SELECT * FROM `users` WHERE `id`='$user->id'");
  while($user = mysql_fetch_object($user1)){
  $data2 = mysql_query("SELECT *,UNIX_TIMESTAMP(`start`) AS `start`,0 FROM `users` where
  `id`='$user->id'");
  $data1 = mysql_fetch_object($data2);
  $date = $user->howlong;
  $timedif1 = $data1->start-3600+$date-time();
  if($data1->start + $date > time()){
   list($h,$min,$sec)=explode(":",date("H:i:s",$timedif1));
  $countdown = $data1->start+$date-time();
}