1. 程式人生 > >PHP 使用do while 實現定時器功能

PHP 使用do while 實現定時器功能

<?php

require "./config.php";

require "./function/function.php";

header("content-type:text/html;charset=utf-8"); 

ignore_user_abort();//關閉瀏覽器仍然執行

set_time_limit(0);//讓程式一直執行下去

$interval=24*3600;//每隔一天時間執行

$yier = 12*24*3600;

$yue = 30*24*3600;  

$date_time = date("Y-m-d H:i:s");

$time = time();   //當前時間

do{

    sleep($interval);//等待時間,進行下一次操作。

    $sql = "select * from ims_shop_jichang_vip where ji_num > 0 or dan_num > 0";   //會員卡vip表

    $arr = query($sql);

    if($arr != null)
    {
	    foreach($arr as $key => $value)
	    {

	    	$openid = $value['open_id'];

	    	$sql = "select * from ims_ewei_shop_vip where jihuo_users = '{$openid}' order by jihuotime desc limit 0,1";

	    	$jihuo_arr = find_query($sql);

	   		if($value['ji_num'] > 0)
	   		{
	   			$get_ji_num = $value['ji_num'];
	   		}
	   		else
	   		{
	   			$get_ji_num = 0;
	   		} 

	   		if($value['dan_num'] > 12)
	   		{
	   			$get_dan_num = 12;
	   		}
	   		else
	   		{
	   			$get_dan_num = $value['dan_num'];
	   		}

	    	$ji_cha_time = $time - $value['top_ji_time'];

	    	$dan_cha_time = $time - $value['top_dan_time'];

	    	$moxing_id = $jihuo_arr['moxing_id'];

	    	$sql = "select id from ims_ewei_shop_goods where moxing_id = {$moxing_id} order by id desc limit 0,1";

	    	$goods_arr = find_query($sql);

	    	$goods_id = $goods_arr['id'];

	    	$uniacid = 2;

	    	file_put_contents("log.txt", 'openid:'.$openid.' ---top_ji_time:'.date("Y-m-d H:i:s",$value['top_ji_time']).' ---top_dan_time:'.date("Y-m-d H:i:s",$value['top_dan_time']).' ---執行時間:'.$date_time."\r\n", FILE_APPEND);  //日誌記錄

	    	if($dan_cha_time > $yier && $dan_num > 0)   //雞蛋定時發貨
	    	{


	    		$sql = "select * from ims_ewei_shop_member_address where openid = '{$openid}' and uniacid = {$uniacid} and isdefault = 1";  //收貨地址

				$address_arr = find_query($sql);

				$address_id = $address_arr['id'];

				$address = serialize($address_arr);

				$ordersn = 'SH'.date("Ymd").rand(000000,999999);

				file_put_contents("dan.txt", 'openid:'.$openid.' ---訂單號:'.$ordersn.' ---時間:'.$date_time."\r\n", FILE_APPEND);  //日誌記錄

	    		$sql = "insert into ims_ewei_shop_order (status,paytype,uniacid,openid,ordersn,price,remark,addressid,address,createtime,paytime,oldprice,grprice) values (1,1,{$uniacid},'{$openid}','{$ordersn}',0,'預設蛋發貨',{$address_id},'{$address}','{$time}','{$time}',0,0)";

					mysql_query($sql);

					$insert_id = mysql_insert_id();

					if($insert_id > 0)
					{
						$sql = "insert into ims_ewei_shop_order_goods (uniacid,orderid,goodsid,price,total,createtime,realprice,oldprice,openid,ji_num,optionname) values ({$uniacid},{$insert_id},$goods_id,0,{$dan_num},'{$time}',0,0,'{$openid}',{$dan_num},'預設蛋發貨')";

						mysql_query($sql);

						$sql = "update ims_shop_jichang_vip set dan_num = dan_num - {$dan_num}, top_dan_time = {$time} where open_id = '{$openid}' and uniacid = {$uniacid}";

						mysql_query($sql);
						
					}

	    	}


	    	if($ji_cha_time > $yue && $ji_num > 0)   //雞定時發貨
	    	{


	    		$sql = "select * from ims_ewei_shop_member_address where openid = '{$openid}' and uniacid = {$uniacid} and isdefault = 1";  //收貨地址

				$address_arr = find_query($sql);

				$address_id = $address_arr['id'];

				$address = serialize($address_arr);

				$ordersn = 'SH'.date("Ymd").rand(000000,999999);

				file_put_contents("ji.txt", 'openid:'.$openid.' ---訂單號:'.$ordersn.' ---時間:'.$date_time."\r\n", FILE_APPEND);  //日誌記錄

	    		$sql = "insert into ims_ewei_shop_order (status,paytype,uniacid,openid,ordersn,price,remark,addressid,address,createtime,paytime,oldprice,grprice) values (1,1,{$uniacid},'{$openid}','{$ordersn}',0,'預設雞發貨',{$address_id},'{$address}','{$time}','{$time}',0,0)";

					mysql_query($sql);

					$insert_id = mysql_insert_id();

					if($insert_id > 0)
					{
						$sql = "insert into ims_ewei_shop_order_goods (uniacid,orderid,goodsid,price,total,createtime,realprice,oldprice,openid,ji_num,optionname) values ({$uniacid},{$insert_id},$goods_id,0,{$ji_num},'{$time}',0,0,'{$openid}',{$ji_num},'預設雞發貨')";

						mysql_query($sql);

						$sql = "update ims_shop_jichang_vip set ji_num = ji_num - {$ji_num}, top_ji_time = {$time} where open_id = '{$openid}' and uniacid = {$uniacid}";

						mysql_query($sql);
					}

	    	}


	    }
    }

    


}while(true);



?>