V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
honkew
V2EX  ›  PHP

有多少会写的

  •  1
     
  •   honkew · 2015-08-24 13:47:58 +08:00 · 6834 次点击
    这是一个创建于 3172 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    用 php 输出

    1.今天的时间戳段

    2.本周的时间戳段

    3.本月的时间戳段

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

    第 1 条附言  ·  2015-08-24 15:40:20 +08:00
    //今日
    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 ))); //结束时间
    }
    第 2 条附言  ·  2015-08-24 15:41:11 +08:00
    用其它语言呢

    python

    java
    第 3 条附言  ·  2015-08-31 10:38:46 +08:00
    <?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 条回复    2015-08-31 10:26:15 +08:00
    ss098
        1
    ss098  
       2015-08-24 13:59:35 +08:00 via iPad
    想了一下第一题,思路大概是 当前时间戳 - (当前时间戳 取余 3600*24 ) 得到 今天开始的时间戳。

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

    有多难
    Kilerd
        3
    Kilerd  
       2015-08-24 14:29:14 +08:00   ❤️ 1
    @ss098 我在想, Unix 时间戳里面有没有包含那个多出来的一秒(传说中的那一分钟的第 60 秒),如果包含了,你这样写就出问题了。
    aprikyblue
        4
    aprikyblue  
       2015-08-24 14:37:22 +08:00
    strtotime ( 'today' )
    strtotime ( 'tomorrow' ) - 1
    iyaozhen
        5
    iyaozhen  
       2015-08-24 14:40:19 +08:00
    @Kilerd 这个方法确实简单粗暴。感觉先求出初始时间戳,然后自己加,更加灵活点。
    abelyao
        6
    abelyao  
       2015-08-24 14:42:51 +08:00
    strtotime ()
    feiyuanqiu
        7
    feiyuanqiu  
       2015-08-24 15:15:04 +08:00
    //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
        8
    Tianpu  
       2015-08-24 15:21:26 +08:00
    定义很容易:

    一天就是 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
        9
    honkew  
    OP
       2015-08-24 15:39:02 +08:00
    //今日
    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
        10
    mhycy  
       2015-08-24 15:45:57 +08:00
    这些一翻手册都有答案的题目有啥意义么?考记忆力?
    honkew
        11
    honkew  
    OP
       2015-08-24 15:49:43 +08:00
    @mhycy 然并卵
    qinglangee
        12
    qinglangee  
       2015-08-24 15:55:09 +08:00
    这种问题是最适合抄网上代码的
    honkew
        13
    honkew  
    OP
       2015-08-24 16:03:39 +08:00
    @qinglangee 是啊,有写时候单机写代码,要命啊
    akstrom
        14
    akstrom  
       2015-08-24 16:19:11 +08:00   ❤️ 1
    //今日
    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
        15
    loading  
       2015-08-24 16:49:55 +08:00 via Android
    不觉得不让查资料,写程序,逗吧!

    这个公司我不去了!
    thinkmore
        16
    thinkmore  
       2015-08-24 17:11:18 +08:00
    ```java
    Date now = new Date ();
    System.out.println (now );

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


    ```
    honkew
        17
    honkew  
    OP
       2015-08-24 17:42:47 +08:00
    @akstrom 我晕乎了,是起始时间 00:00 到结束时间的 00:00 ,难道我想错了
    akstrom
        18
    akstrom  
       2015-08-24 18:04:34 +08:00
    呵呵.............
    a591826944
        19
    a591826944  
       2015-08-24 18:16:23 +08:00
    这个有意义么
    a591826944
        20
    a591826944  
       2015-08-24 18:16:40 +08:00
    V2 真是 越来越水了
    wingoo
        21
    wingoo  
       2015-08-24 18:19:01 +08:00
    time ()啊
    86500 是一天啊, 随便加减啊
    jhdxr
        22
    jhdxr  
       2015-08-25 14:11:44 +08:00
    1. 这真不难
    2. 你的题目没有说明哪个时区。。。这很关键。。。
    honkew
        23
    honkew  
    OP
       2015-08-25 14:23:53 +08:00
    @jhdxr 亚洲 asia
    honkew
        24
    honkew  
    OP
       2015-08-25 14:24:53 +08:00
    中国 - 北京 - 北京
    honkew
        25
    honkew  
    OP
       2015-08-31 10:26:15 +08:00
    @akstrom 感谢指出
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2576 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 14:55 · PVG 22:55 · LAX 07:55 · JFK 10:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.