如何从 Unix 时间戳 (PHP) 获取星期几?

发布于 2024-08-25 23:13:19 字数 62 浏览 3 评论 0原文

如何从 PHP 中的 Unix 时间戳获取日期 (1-7)?我还需要日期 (1-31) 和月份 (1-12)。

How do I get the day (1-7) from a Unix timestamp in PHP? I also need the day date (1-31) and month (1-12).

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

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

发布评论

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

评论(5

深陷 2024-09-01 23:13:19

您可以使用 date() 函数

$weekday = date('N', $timestamp); // 1-7
$month = date('m', $timestamp); // 1-12
$day = date('d', $timestamp); // 1-31

You can use date() function

$weekday = date('N', $timestamp); // 1-7
$month = date('m', $timestamp); // 1-12
$day = date('d', $timestamp); // 1-31
玩物 2024-09-01 23:13:19

请参阅 http://docs.php.net/getdate

例如

$ts = time(); // could be any timestamp
$d=getdate($ts);

echo 'day of the week: ', $d['wday'], "\n";
echo 'day of the month: ', $d['mday'], "\n";
echo 'month: ', $d['mon'], "\n";

see http://docs.php.net/getdate

e.g.

$ts = time(); // could be any timestamp
$d=getdate($ts);

echo 'day of the week: ', $d['wday'], "\n";
echo 'day of the month: ', $d['mday'], "\n";
echo 'month: ', $d['mon'], "\n";
旧故 2024-09-01 23:13:19

这是您需要的 date() 函数。

您可以从 PHP 手册中获取更多详细信息,但简而言之,这里是您需要的功能。

date('N', $timestamp);
//numeric representation of the day of the week

date('j', $timestamp);
//Day of the month without leading zeros

date('n', $timestamp);
//Numeric representation of a month, without leading zeros

It's the date() function you're after.

You can get more details from the PHP manual but in a nutshell here are the functions you need.

date('N', $timestamp);
//numeric representation of the day of the week

date('j', $timestamp);
//Day of the month without leading zeros

date('n', $timestamp);
//Numeric representation of a month, without leading zeros
可是我不能没有你 2024-09-01 23:13:19

使用前面所述的 date 函数,并将 $timestamp 作为第二个参数:

$weekday = date('N', $timestamp); // 1 = Monday to 7 = Sunday
$month = date('m', $timestamp); // 1-12 = Jan-Dec
$day = date('d', $timestamp); // 1-31, day of the month

并非所有 PHP 版本都能很好地处理负时间戳。我的经验是,使用新的 DateTime对象。

Use the date function as stated before, with your $timestamp as the second argument:

$weekday = date('N', $timestamp); // 1 = Monday to 7 = Sunday
$month = date('m', $timestamp); // 1-12 = Jan-Dec
$day = date('d', $timestamp); // 1-31, day of the month

Not all PHP versions play nice with negative timestamps. My experience is that timestamps dating back to before the UNIX epoch fare better with the new DateTime object.

尴尬癌患者 2024-09-01 23:13:19
print "Week".date('N')."\n";
print "day of month " .date('d')."\n";
print "month ".date('m')."\n";
print "Week".date('N')."\n";
print "day of month " .date('d')."\n";
print "month ".date('m')."\n";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文