计算日期时间 自动加1天 PHP计算闰年 java与PHP时间戳对比区别

昨天写一个同步数据库的模块  从一个数据库同步到另外一个数据库,因为数据较多,不可能一次性全部搬迁过去,所以就按照每天搬迁!

写了一个 模块,点击加1,只要点击一次,自动从A数据库取出1天的数据, 并插入到B数据库,

在代码中你可能看到 计算时间的时候用到了 时间戳,并且时间戳加了好多 ‘0’ 是因为那是sqlserver 是公司java同事写的,为了PHP计算的时间戳与之匹配,

所以通过加‘0’之后  对比 时间戳取出一天的数据!

这里顺便整理一下 java和PHP之间的时间戳的区别!

java 时间戳和PHP时间戳 的转换 php time()
总结一下java 时间戳和PHP时间戳 的转换问题:
由于精度不同,导致长度不一致,直接转换错误。
JAVA时间戳长度是13位,如:1294890876859
PHP时间戳长度是10位, 如:1294890859

主要最后三位的不同,JAVA时间戳在PHP中使用,去掉后三位,如:1294890876859-> 1294890876 结果:2011-01-13 11:54:36

    echo date('Y-m-d H:i:s','1294890876');

复制代码
PHP时间戳在JAVA中使用,最后加三位,用000补充,如:1294890859->1294890859000
结果:2011-01-13 11:54:19

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String dateTime = df.format(1294890859000L);               
    System.out.println(df);

复制代码

总结一下java时间戳和PHP时间戳 的转换问题:
由于精度不同,导致长度不一致,直接转换错误。
JAVA时间戳长度是13位,如:1294890876859
PHP时间戳长度是10位, 如:1294890859

主要最后三位的不同,JAVA时间戳在PHP中使用,去掉后三位,如:1294890876859-> 1294890876 结果:2011-01-13 11:54:36
echo date('Y-m-d H:i:s','1294890876');
 

PHP时间戳在JAVA中使用,最后加三位,用000补充,如:1294890859->1294890859000
结果:2011-01-13 11:54:19
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateTime = df.format(1294890859000L);               
System.out.println(df);

下面整理下我自己写的代码!方便日后查阅,类是用CI写的!

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 14-12-23
 * Time: 下午4:27
 */

class Sync extends My_Controller {

    /**
     * @var hr伴侣数据库的实例
     */
    private $hrdb;

    private $year ;

    private $month ;

    private $date ;

    private $month_31 = array(1,3,5,7,8,10,12);

    private $month_30 = array(4,6,9,11);

    private $startTimeStamp;
    private $recursionCount = 0;//递归计数

    public function __construct() {
        parent::__construct();
        header("Content-type:text/html;charset=utf-8");
        date_default_timezone_set('PRC');
        $this->load->model('sync_mdl');
        $this->hrdb = $this->sync_mdl->getSqlServer();
    }

    public function action() {

        $beginYear  = $this->input->post('beginYear');
        $beginMonth = $this->input->post('beginMonth');
        $beginDay   = $this->input->post('beginDay');
        $endYear    = $this->input->post('endYear');
        $endMonth   = $this->input->post('endMonth');
        $endDay     = $this->input->post('endDay');


       if( !empty($beginYear) && !empty($beginMonth) && !empty($beginDay) && empty($endYear) && empty($endMonth) && empty($endDay)) {
        //自定义开始同步时间,默认同步1天
           $this->year = $beginYear; $this->month = $beginMonth; $this->date = $beginDay;
           $this->startTimeStamp = $this->getTimeStamp($this->year, $this->month, $this->date);

           $result = $this->fetchOneDayData($this->startTimeStamp, $this->computeTime());

       } else if( !empty($beginYear) && !empty($beginMonth) && !empty($beginDay) && !empty($endYear) && !empty($endMonth) && !empty($endDay) ) {
           //自定义开始 结束的同步时间
           $endTimeStamp = $this->getTimeStamp($endYear, $endMonth, $endDay);

           $result = $this->fetchOneDayData( $this->startTimeStamp, $endTimeStamp );

       } else {

           echo '<p style="color:red">时间不能为空</p>';

       }

         //if(!empty( array('mobile')) ) {
             //就用电话创建账号
         //} else if(email 不为空) {
            //就用邮箱创建账号
        //} else {
            //不做任何操作
        //}

            //如果数据不为空,判断mobile 或者 email 是否存在于小职了

                 //以上通过,便进行插入到小职了的数据库

       // echo $this->startTimeStamp; echo '<br/>';
        //echo $this->computeTime();
        //当前取值的数据;

        $data['year']  = $this->year;
        $data['month'] = $this->month;
        $data['day']   = $this->date;
    $this->load->view('sync', $data);
        echo '<pre/>';
        if( !empty($result) ) {
            print_r($result);
        } else if(isset($result) && $result == 'today') {
            echo '已经到今天了!';
        } else if( empty($rs) ) {
            echo '本日无数据可同步';
        }
    }

    /**取出一天的数据, 只要计算的当天数据为空,递归调用,直到当天有数据
     * @param $startTimeStamp   开始时间戳
     * @param $endTimeStamp     结束时间戳
     * @return mixed            返回一天的数据
     */
    private function fetchOneDayData($startTimeStamp, $endTimeStamp) {

        if($startTimeStamp == $endTimeStamp) {

            return 'today';

        } else {

            $rs = $this->fetchHrData($startTimeStamp, $endTimeStamp);
            if(empty($rs)) {
                return array();
            } else if( !empty($rs) ) {
                return $rs;
            }

//            if(!empty($rs)) {
//
//               return $rs;
//
//            } else if(empty($rs)) {
//                $this->recursionCount = $this->recursionCount + 1;
//                $this->startTimeStamp = $endTimeStamp;
//                if($this->recursionCount < 50) {
//                    //echo '<strong>'.$this->recursionCount.'</strong>';
//                    //如果数据不为空的时候,这里接收到本身调用自己返回的数组,并返回
//                    $re = $this->fetchOneDayData($this->startTimeStamp, $this->computeTime());
//                    return $re;
//
//                } else {
//                    return '超过50天没有数据了';
//                }

//            }
        }//end else
    }


    /**根据时间取出每天数据
     * @param $beginTime
     * @param $endTime
     */
    private function fetchHrData($beginTime, $endTime) {

        $sql = "SELECT truename,pubdate FROM resume_cn WHERE pubdate >= $beginTime AND pubdate <= $endTime";
        $query = $this->hrdb->query($sql);
        $rs = $query->result_array();
        //echo '<pre/>';
        return $rs;
//        print_r($rs);
    //$this->test($rs[9]['pubdate'])
    }


    /**计算年份是否是闰年,如果是闰年 2月份是29天  平年是28天, 每调用一次这个函数,天数增 加1天
     * @return string  时间戳,是经过计算的,前加 '00'  后加'000' ,为了与hr伴侣的时间戳相匹配
     */
    private function computeTime() {
        if (($this->year % 4 == 0 && $this->year % 100 != 0) || ($this->year % 400 == 0)) {
            $this->computeTimeDate(29);
            return $this->getTimeStamp($this->year, $this->month, $this->date);
        } else {
            $this->computeTimeDate(28);
            return $this->getTimeStamp($this->year, $this->month, $this->date);
        }
    }

    /**根据2月份是多少天,计算日期时间,
     * @param $Feb  2月的天数
     */
    private function computeTimeDate($Feb) {

        if ($this->month == 2) {

            if($this->date >= 1 && $this->date <= $Feb) {
                $this->date = $this->date + 1;
            } else if($this->date > $Feb) {
               $this->computeDateMonth();
            } else {
                die('2月份天数不在正常范围内');
            }

        } else if( in_array($this->month, $this->month_30) ) {

            if( $this->date >= 1 && $this->date < 30 ) {
                $this->date = $this->date + 1;
            } else if($this->date >= 30){
                $this->computeDateMonth();
            } else {
                die('30天的月份天数不在正常范围内');
            }

        } else if(in_array($this->month, $this->month_31)) {

            if( $this->date >= 1 && $this->date < 31 ) {
                $this->date = $this->date + 1;
            } else if( $this->date >= 31 ){
                $this->computeDateMonth();
            } else {
                die('31天的月份天数不在正常范围内');
            }

        } else {
//            echo $this->month;
            die('年月日不正确');
        }
    }

    /**
     * 计算一年当中的月份是否为12个月,如果超过12 就让年份 +1
     */
    private function computeDateMonth() {
        if($this->month >= 1 && $this->month < 12) {
            $this->month = $this->month + 1;
            $this->date = 1;
        } else if($this->month == 12) {
            if( $this->year == date('Y', time()) ) {
               return;
            } else {
                $this->year = $this->year + 1;
                $this->month = 1;
                $this->computeTime();
            }
        } else {
            die('computeDateMonth函数计算错误');
        }
    }

    /**获取用于与HR数据库对比的时间戳,如果定义了年月日 就用定义的时间,否则就取当前时间
     * @param $y           年
     * @param $m           月
     * @param $d           日
     * @param $h           小时
     * @param $i           分钟
     * @param $s           秒
     * @return string      时间戳
     */
    private function getTimeStamp($y = '', $m = '', $d = '', $h = '0', $i = '0', $s = '0') {
        if( $y && $m && $d) {
            return '00' . mktime( $h, $i, $s, $m, $d, $y ) . '000';
        } else {
            return '00' . time() . '000';
        }
    }




}




/* End of file Sync.php */
/* Location: controllers/sync.php */
原文地址:https://www.cnblogs.com/itafter/p/4183881.html