PHP时间相减少的情况一览。。。date(), time(),mktime()

jackxiang 2008-10-15 15:36 | |
Crontab跑昨天的DB时间:

$date = date('Y-m-d',strtotime("-1 day"));//昨天
$date = date('Ymd',strtotime("-1 day"));

结果:
2011-03-15
20110315


$fromDate1=mktime(9,21,0,10,23,2009);//开始时间 --->  时,分,秒 月,日,年
         echo date("M-d-Y H:i:s",$fromDate1);


Oct-23-2009 09:21:00

$date_d=date("d")-1;//天数减一天,数字想减少罢了。
$starttime = date("Y-m-d 00:00:00", strtotime("-3 day"));//天数减少3天,蕴含日期相见月份相减少。  date("Y-m-d H:i:s")

$lastread = time()-3*24*60*60;//减少是秒数,看下文sql构造:)
echo date("Y-m-d H:i:s",$lastread);


计算秒数(60 秒):

$date_time = time();
$date_time2 = time();
$date_time2-$date_time >= 60  //60秒


反过来,由Y-m-d H:i:s变为unix时间秒:
1)Mysql:
UNIX_TIMESTAMP(etime) > UNIX_TIMESTAMP())
http://www.cnblogs.com/showker/archive/2010/03/18/1689054.html
2)PHP:
用strtotime(格式)可以获取指定时间的时间戳

<?php
require_once ("/data0/vshare/app/rank/config.php");
define("USER_SEARCH_IASK_LOG_PATH", "/data0/vshare/logs/user_search/iasks/");//删除的用户及视频日志目录(爱问)
echo "程序于 " . date("Y-m-d H:i:s") . " 开始执行:\n";
$date_d=date("d")-1;
if($date_d<10)
{
  $date_d="0".$date_d;
}
$iask_filename = USER_SEARCH_IASK_LOG_PATH.date("Ym")."/".$date_d."/boke_ay_" . date("Ymd", strtotime("-1 day")) .".log";
$iask_filename_html = USER_SEARCH_IASK_LOG_PATH.date("Ym")."/".$date_d."/boke_ay_" . date("Ymd", strtotime("-1 day")) .".html";
$conn_db_3306_r = @mysql_connect(DBHOST_VIDEO_R_aoyun, DBUSER_VIDEO_R_aoyun, DBPASSWD_VIDEO_R_aoyun) or
    die(DBHOST_VIDEO_R_aoyun.DBUSER_VIDEO_R_aoyun.DBPASSWD_VIDEO_R_aoyun."Can't connect to 3306 port readonly database");//长连接
//统计昨天登录过的用户个人中心信息
echo "正在从MySQL数据库中取出所有的用户userid,请稍候....";
//exit(0);
//$select_dt_user_r = @mysql_select_db("dt_video", $conn_db_3306_r) or die("Can't select database");
$select_dt_user_r = @mysql_select_db("dt_manage", $conn_db_3306_r) or die("Can't select database");
//$sql_tb_usertotal_r = "SELECT videoid,up_userid FROM tb_video_total where activeid = 0 limit 10";/*上线后打开*/
$starttime = date("Y-m-d 00:00:00", strtotime("-3 day"));
$endtime = date("Y-m-d 00:00:00");
$sql_tb_usertotal_r = "select videoid,up_userid from tb_video_activity where (adddate>'".$starttime."' or appr_time>'".$starttime."') and adddate<'".$endtime."' and appr_status=1 and active_status in(1,0) and conv_status=1 and del_status=0 and activeid=128;";/*上线后打开*/
$query_tb_usertotal_r = mysql_query($sql_tb_usertotal_r, $conn_db_3306_r) or die("Query Error:" .
    mysql_error() . "\n");
echo  $sql_tb_usertotal_r."\n";
echo "\n[取出所有的用户userid:完成]\n";
$i=0;
echo $iask_filename."\n";

if(file_exists($iask_filename))
{
    unlink($iask_filename);
}

while ($row_tb_usertotal_r = mysql_fetch_assoc($query_tb_usertotal_r))
{
     if (!($i % 2000))
    {
      sleep(1);        
    }
  $vid = $row_tb_usertotal_r['videoid'];
  $uid = $row_tb_usertotal_r['up_userid'];
  $result['vid'] = $vid;
  $result['uid'] = $uid;
  /*  
  echo "VID:". $result['vid'] = $vid."\n";
  echo "UID:".$result['uid'] = $uid."\n";
  */
  $content = getContent($result,$i);
  us_writeFile($content, $iask_filename, USER_SEARCH_IASK_LOG_PATH .date("Ym")."/".$date_d);
  $i=$i+1;
  
    
}
if (file_exists($iask_filename) && is_readable($iask_filename))
{
  echo "正在向爱问推送数据,请稍候....\n";
  //pclose(popen("touch {$iask_filename} && chmod 0664 {$iask_filename} && /usr/bin/rsync -vzrtup {$iask_filename} 10.69.2.48::usersource_video/",
        //"r"));
  $command="/usr/bin/rsync -avgt --progress --password-file=/usr/local/etc/rsyncd.secrets " . USER_SEARCH_IASK_LOG_PATH ."*"." backupuser@219.142.118.48::boke_2008_sum_olympic_video";
  echo $command."\n";
  pclose(popen("touch {$iask_filename} && chmod 0664 {$iask_filename} && $command",
        "r"));
  
  $command="/usr/bin/rsync -avgt --progress --password-file=/usr/local/etc/rsyncd.secrets " . USER_SEARCH_IASK_LOG_PATH ."*"." backupuser@172.16.153.70::olympic_video/";
  pclose(popen("touch {$iask_filename} && chmod 0664 {$iask_filename} && $command",
        "r"));
    $command="/usr/bin/rsync -avu " . $iask_filename ." 172.16.6.198::vblog/";
    pclose(popen("touch {$iask_filename} && chmod 0664 {$iask_filename} && $command",
        "r"));
    $command="cp {$iask_filename} {$iask_filename_html}";
    pclose(popen($command, "r"));
    $command="/usr/bin/rsync -avu {$iask_filename_html} {$iask_filename} 10.68.1.30::v_blog_sina_com_cn/htdocs/ay_data/";
    pclose(popen($command, "r"));
    $command="/usr/bin/rsync -avu {$iask_filename_html} {$iask_filename} 10.68.1.71::v_blog_sina_com_cn/htdocs/ay_data/";
    pclose(popen($command, "r"));
  
  echo "[推送完成]\n";
}



echo "\n[完成]\n";


echo "程序于 " . date("Y-m-d H:i:s") . " 执行结束。\n\n";

function us_path_validate($path)
{
    $arraypath = split("\/+", $path);
    $tmppath = "";
    for ($i = 0; $i < count($arraypath); $i++)
    {
        if ("" == $arraypath[$i])
            continue;
        $tmppath .= (0 == $i ? "":"/") . $arraypath[$i];
        if (!is_dir($tmppath))
        {
            if (!mkdir($tmppath, 0777))
            {
                return false;
            }
        }
    }
    return true;
}

function us_writeFile($content, $filename, $dirpath)
{
    if (!us_path_validate($dirpath))
        exit();
    $fp = fopen($filename, "a+");
    if (!@fwrite($fp, $content . "\n"))
    {
        @fclose($fp);
        return false;
    }
    fclose($fp);
    return true;
}

function getContent($result,$i)
{
  /*
         if($i==0)
  {
    $str  = "VID\tUID\t日期\n";
          $str .= $result['vid'] . "\t";//视频总量
          $str .= $result['uid'] . "\t";//userid,用户UID
          $str .= date("Y-m-d",strtotime("-1 day")). "";
    return $str;
  }else{
  */
    $str  = $result['vid'] . "\t";//视频总量
          $str .= $result['uid'] . "\t";//userid,用户UID
          $str .= date("Y-m-d",strtotime("-1 day"))   . "";
    return $str;
   //    }
}
?>



function GetUnreadNum($uid)
{
  $cache = $this->_connectCache();
  $num = $cache->get(CACHE_SYSLOG."_new_".$uid);
  if($num===false)
  {
    $lastread = $cache->get(CACHE_SYSLOG."_read_".$uid);
    if($lastread===false) $lastread = time()-3*24*60*60;

    $db = $this->_connectDb(0);
    $sql = "select count(*) from syslog where uid='$uid' and time>FROM_UNIXTIME($lastread)";
    $st = $db->query($sql);
    $num = $st->fetchColumn()+0;
    $cache->set(CACHE_SYSLOG."_new_".$uid,$num);
  }
return $num;
}

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/1310/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!


最后编辑: jackxiang 编辑于2014-8-5 10:31
评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]