honkew
V2EX  ›  PHP

有多少会写的

  •  1
     
  •   honkew · Aug 24, 2015 · 8018 views
    This topic created in 4098 days ago, the information mentioned may be changed or developed.

    基础问题-不允许抄网上代码,试试吧,骚年!

    用 php 输出

    1.今天的时间戳段

    2.本周的时间戳段

    3.本月的时间戳段

    4.今年所有月份的时间戳段

    Supplement 1  ·  Aug 24, 2015
    //今日
    strtotime (date ('Y-m-d'));
    strtotime ("+1 day",strtotime (date ("Y-m-d")));
    //本周
    strtotime ('Mon');
    strtotime ('Sun');
    //本月
    strtotime (date ('Y-m-01'));
    strtotime (date ('Y-m-t'));
    //本年每月
    $_time = strtotime (date ('Y-1-1',time ()));
    for ($i=0;$i<12;$i++){
    strtotime ("+ {$i}month", $_time ); //起始时间
    strtotime (date ('Y-m-t',strtotime ("+ {$i}month", $_time ))); //结束时间
    }
    Supplement 2  ·  Aug 24, 2015
    用其它语言呢

    python

    java
    Supplement 3  ·  Aug 31, 2015
    <?php

    date_default_timezone_set ('Asia/Shanghai');

    //今日
    $stime = strtotime (date ('Y-m-d'));
    $etime = strtotime ("+1 day",strtotime (date ("Y-m-d"))-1 );
    //昨日
    $stime = strtotime ("-1 day",strtotime (date ("Y-m-d")));
    $etime = strtotime (date ('Y-m-d'))-1;
    //本周
    $stime = strtotime ('Mon',time ());
    $etime = strtotime ('Sun',strtotime (date ('Y-m-d 23:59:59')));
    //本月
    $stime = strtotime (date ('Y-m-01'));
    $etime = strtotime (date ('Y-m-t 23:59:59'));

    //从 00:00:00 到 23:59:59 >=00:00:00 <23:59:59
    25 replies  •  2015-08-31 10:26:15 +08:00
    ss098
    ss098
    Aug 24, 2015 via iPad
    1
    想了一下第一题,思路大概是 当前时间戳 - (当前时间戳 取余 3600*24 ) 得到 今天开始的时间戳。

    然后加上 3600 * 24 得到今天结束的时间戳,剩下的题目类推。
    Kilerd
    Kilerd
    Aug 24, 2015
    2
    时间戳转成时间字符串
    然后再替换一些数据(1 、时分秒替换成 00:00:00 和 23:59:59 ; 以后类推),再转回时间戳就好了

    有多难
    Kilerd
    Kilerd
    Aug 24, 2015❤️1
    3
    @ss098 我在想, Unix 时间戳里面有没有包含那个多出来的一秒(传说中的那一分钟的第 60 秒),如果包含了,你这样写就出问题了。
    aprikyblue
    aprikyblue
    Aug 24, 2015
    4
    strtotime ( 'today' )
    strtotime ( 'tomorrow' ) - 1
    iyaozhen
    iyaozhen
    Aug 24, 2015
    5
    @Kilerd 这个方法确实简单粗暴。感觉先求出初始时间戳,然后自己加,更加灵活点。
    abelyao
    abelyao
    Aug 24, 2015
    6
    strtotime ()
    feiyuanqiu
    feiyuanqiu
    Aug 24, 2015
    7
    //1.今天的时间戳段
    $todayBegin = strtotime ('today');
    $todayEnd = strtotime ('tomorrow')-1;

    //2.本周的时间戳段
    $weekBegin = strtotime ('this monday');
    $weekEnd = strtotime ('next monday')-1;

    //3.本月的时间戳段
    $monthBegin = mktime (0, 0, 0, date ('m'), 1 );
    $monthEnd = mktime (0, 0, 0, date ('m', strtotime ('next month')), 1 )-1;

    //4.今年所有月份的时间戳段
    $monthInTheYear = array ();
    foreach (range (1, 12 ) as $month ) {
    $begin = mktime (0, 0, 0, $month, 1 );
    $end = strtotime (date ('Y-m-d', $begin ) . '+1 month')-1;
    $monthInTheYear[$month] = array (
    'begin' => $begin,
    'end' => $end,
    );
    }
    exit;
    Tianpu
    Tianpu
    Aug 24, 2015
    8
    定义很容易:

    一天就是 Y-m-d 一样的时间戳
    一月就是 Y-m 一样的时间戳
    一年就是 Y 一样的时间戳

    必定存在的:
    每日的 Y-m-d 00:00:00 (需要检查当月的 Y-m-d 是否存在)
    每月的 Y-m-01 00:00:00 (需要检查下当月的 Y-m 是否存在)
    每年的 Y-01-01 00:00:00 (需要检查当年的 Y 是否存在)

    已知这些东西,使用筛法都能快速筛出来了
    比如以 step=1s 筛 1000 年,腌起来风干了吃
    honkew
    honkew
    OP
    Aug 24, 2015
    9
    //今日
    strtotime (date ('Y-m-d'));
    strtotime ("+1 day",strtotime (date ("Y-m-d")));
    //本周
    strtotime ('Mon');
    strtotime ('Sun');
    //本月
    strtotime (date ('Y-m-01'));
    strtotime (date ('Y-m-t'));
    //本年每月
    $_time = strtotime (date ('Y-1-1',time ()));
    for ($i=0;$i<12;$i++){
    strtotime ("+ {$i}month", $_time ); //起始时间
    strtotime (date ('Y-m-t',strtotime ("+ {$i}month", $_time ))); //结束时间
    }
    mhycy
    mhycy
    Aug 24, 2015
    10
    这些一翻手册都有答案的题目有啥意义么?考记忆力?
    honkew
    honkew
    OP
    Aug 24, 2015
    11
    @mhycy 然并卵
    qinglangee
    qinglangee
    Aug 24, 2015
    12
    这种问题是最适合抄网上代码的
    honkew
    honkew
    OP
    Aug 24, 2015
    13
    @qinglangee 是啊,有写时候单机写代码,要命啊
    akstrom
    akstrom
    Aug 24, 2015❤️1
    14
    //今日
    strtotime (date ('Y-m-d'));
    strtotime ("+1 day",strtotime (date ("Y-m-d")));
    //本周
    strtotime ('Mon');
    strtotime ('Sun');
    //本月
    strtotime (date ('Y-m-01'));
    strtotime (date ('Y-m-t'));
    //本年每月
    $_time = strtotime (date ('Y-1-1',time ()));
    for ($i=0;$i<12;$i++){
    strtotime ("+ {$i}month", $_time ); //起始时间
    strtotime (date ('Y-m-t',strtotime ("+ {$i}month", $_time ))); //结束时间
    }

    以上这些就是对的吗?

    1.今天的时间戳段(多了一秒)

    2.本周的时间戳段(少了 23:59:59 )

    3.本月的时间戳段(少了 23:59:59 )

    4.今年所有月份的时间戳段(少了 23:59:59 )
    loading
    loading
    Aug 24, 2015 via Android
    15
    不觉得不让查资料,写程序,逗吧!

    这个公司我不去了!
    thinkmore
    thinkmore
    Aug 24, 2015
    16
    ```java
    Date now = new Date ();
    System.out.println (now );

    #其他的通过 calendar 了#
    Calendar cal = Calendar.getInstance ();


    ```
    honkew
    honkew
    OP
    Aug 24, 2015
    17
    @akstrom 我晕乎了,是起始时间 00:00 到结束时间的 00:00 ,难道我想错了
    akstrom
    akstrom
    Aug 24, 2015
    18
    呵呵.............
    a591826944
    a591826944
    Aug 24, 2015
    19
    这个有意义么
    a591826944
    a591826944
    Aug 24, 2015
    20
    V2 真是 越来越水了
    wingoo
    wingoo
    Aug 24, 2015
    21
    time ()啊
    86500 是一天啊, 随便加减啊
    jhdxr
    jhdxr
    Aug 25, 2015
    22
    1. 这真不难
    2. 你的题目没有说明哪个时区。。。这很关键。。。
    honkew
    honkew
    OP
    Aug 25, 2015
    23
    @jhdxr 亚洲 asia
    honkew
    honkew
    OP
    Aug 25, 2015
    24
    中国 - 北京 - 北京
    honkew
    honkew
    OP
    Aug 31, 2015
    25
    @akstrom 感谢指出
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2430 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 115ms · UTC 12:30 · PVG 20:30 · LAX 04:30 · JFK 07:30
    ♥ Do have faith in what you're doing.