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

有多少会写的

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

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

    用 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     Solana     6114 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 36ms UTC 03:12 PVG 11:12 LAX 20:12 JFK 23:12
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86