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

时间:2022-10-21 10:05:46

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).

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

5 个解决方案

#1


39  

You can use date() function

您可以使用date()函数

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

#2


6  

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

见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";

#3


6  

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

这是你所追求的date()函数。

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

您可以从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

#4


2  

print "Week".date('N')."\n";
print "day of month " .date('d')."\n";
print "month ".date('m')."\n";

#5


1  

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

使用前面所述的日期函数,将$ 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

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.

并非所有PHP版本都可以使用负时间戳。我的经验是,使用新的DateTime对象,可以追溯到UNIX时代之前的时间戳更好。

#1


39  

You can use date() function

您可以使用date()函数

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

#2


6  

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

见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";

#3


6  

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

这是你所追求的date()函数。

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

您可以从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

#4


2  

print "Week".date('N')."\n";
print "day of month " .date('d')."\n";
print "month ".date('m')."\n";

#5


1  

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

使用前面所述的日期函数,将$ 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

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.

并非所有PHP版本都可以使用负时间戳。我的经验是,使用新的DateTime对象,可以追溯到UNIX时代之前的时间戳更好。