如何获取一周中某一天的时间戳?

发布于 2024-10-19 09:01:42 字数 190 浏览 2 评论 0原文

基本上,我有将当前/未来工作日转换为时间戳的要求。

例子:

today     = Thu, 24 Feb 2010
weekday   = Tue
next date = Tue,  1 Mar 2010
cur stamp = 1267016400
new stamp = 1267448400

Basically, I have this requirement to convert a present/future weekday to a timestamp.

Example:

today     = Thu, 24 Feb 2010
weekday   = Tue
next date = Tue,  1 Mar 2010
cur stamp = 1267016400
new stamp = 1267448400

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少女的英雄梦 2024-10-26 09:01:42

strtotime('Tue') 将返回下周二的时间戳。

strtotime('Tue', $time) 将返回给定时间戳的下一个星期二的时间戳。

strtotime('Tue') will return the timestamp of the next Tuesday.

strtotime('Tue', $time) will return the timestamp of the next Tuesday from the given timestamp.

So要识趣 2024-10-26 09:01:42

这是我最终得到的代码:

/**
 * @param $weekday string The weekday in short (3 letter) format, eg: "Mon" or "Tue".
 * @return integer The calculated timestamp.
 */
function next_weekday_to_stamp($weekday,$today=null){
    if(!$today)$today=time();
    $range = array('Mon','Tue','Wed','Thu','Fri','Sat','Sun','Mon','Tue','Wed','Thu','Fri','Sat','Sun');
    $days = array_search(date('D'), $range);
    $range = array_slice($range, $days);
    $days = array_search($weekday, $range);
    return strtotime('+'.$days.' days', $today);
}

谈论过度设计!

无论如何,如果有人想在过去(上个工作日)使用它,它可以很容易地适应。

This is the code I ended up with:

/**
 * @param $weekday string The weekday in short (3 letter) format, eg: "Mon" or "Tue".
 * @return integer The calculated timestamp.
 */
function next_weekday_to_stamp($weekday,$today=null){
    if(!$today)$today=time();
    $range = array('Mon','Tue','Wed','Thu','Fri','Sat','Sun','Mon','Tue','Wed','Thu','Fri','Sat','Sun');
    $days = array_search(date('D'), $range);
    $range = array_slice($range, $days);
    $days = array_search($weekday, $range);
    return strtotime('+'.$days.' days', $today);
}

Talk about over-engineering it out!

Anyway, if someone wants to use this in the past (last weekday), it can be easily adapted.

星光不落少年眉 2024-10-26 09:01:42

你试过吗

strtotime("+7 days", strtotime("Thu, 24 Feb 2010"))

have you tried

strtotime("+7 days", strtotime("Thu, 24 Feb 2010"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文