有多少会写的

2015-08-24 13:47:58 +08:00
 honkew

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

用 php 输出

1.今天的时间戳段

2.本周的时间戳段

3.本月的时间戳段

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

6856 次点击
所在节点    PHP
25 条回复
ss098
2015-08-24 13:59:35 +08:00
想了一下第一题,思路大概是 当前时间戳 - (当前时间戳 取余 3600*24 ) 得到 今天开始的时间戳。

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

有多难
Kilerd
2015-08-24 14:29:14 +08:00
@ss098 我在想, Unix 时间戳里面有没有包含那个多出来的一秒(传说中的那一分钟的第 60 秒),如果包含了,你这样写就出问题了。
aprikyblue
2015-08-24 14:37:22 +08:00
strtotime ( 'today' )
strtotime ( 'tomorrow' ) - 1
iyaozhen
2015-08-24 14:40:19 +08:00
@Kilerd 这个方法确实简单粗暴。感觉先求出初始时间戳,然后自己加,更加灵活点。
abelyao
2015-08-24 14:42:51 +08:00
strtotime ()
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
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
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
2015-08-24 15:45:57 +08:00
这些一翻手册都有答案的题目有啥意义么?考记忆力?
honkew
2015-08-24 15:49:43 +08:00
@mhycy 然并卵
qinglangee
2015-08-24 15:55:09 +08:00
这种问题是最适合抄网上代码的
honkew
2015-08-24 16:03:39 +08:00
@qinglangee 是啊,有写时候单机写代码,要命啊
akstrom
2015-08-24 16:19:11 +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 ))); //结束时间
}

以上这些就是对的吗?

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

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

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

4.今年所有月份的时间戳段(少了 23:59:59 )
loading
2015-08-24 16:49:55 +08:00
不觉得不让查资料,写程序,逗吧!

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

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


```
honkew
2015-08-24 17:42:47 +08:00
@akstrom 我晕乎了,是起始时间 00:00 到结束时间的 00:00 ,难道我想错了
akstrom
2015-08-24 18:04:34 +08:00
呵呵.............
a591826944
2015-08-24 18:16:23 +08:00
这个有意义么
a591826944
2015-08-24 18:16:40 +08:00
V2 真是 越来越水了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/215541

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX