如何将周数和年转换为unix时间戳? [英] How to convert week number and year into unix timestamp?

查看:188
本文介绍了如何将周数和年转换为unix时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将日期分为周数和年份,然后将那个周数转换回unix时间戳.我该怎么做呢?

I'm trying to group together dates into a week number and year, and then I want to convert that week number back into a unix timestamp. How can I go about doing this?

推荐答案

我假设您使用的是ISO 8601周编号,并且希望使用ISO 8601周的第一天,例如Week 1 of 2011返回January 3 2011.

I assume you are using ISO 8601 week numbers, and want the first day of a ISO 8601 week so that e.g. Week 1 of 2011 returns January 3 2011.

strtotime可以使用{YYYY}W{WW}格式直接执行此操作:

strtotime can do this out of the box using the {YYYY}W{WW} format:

echo date("Y-m-d", strtotime("2011W01")); // 2011-01-03

请注意,周号必须为两位位.

Note that the week number needs to be two digits.

可悲的是,DateTime::createFromFormat,一种新颖的PHP 5处理日期的方式,似乎无法解析此类信息-它没有星期"占位符.

Shamefully, DateTime::createFromFormat, the fancy new PHP 5 way of dealing with dates, seems unable to parse this kind of information - it doesn't have a "week" placeholder.

这篇关于如何将周数和年转换为unix时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆