在 PHP 中获取当天的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2306460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:58:57  来源:igfitidea点击:

Getting the date for current day in PHP

phpdatetimestamp

提问by anurag-jain

I want to get the date for current day in php. what i tried is here...

我想在 php 中获取当天的日期。我试过的就在这里...

echo $x."<br>";
echo date("D",$x)."<br>";

But the output was

但输出是

21-02-10
Thu

It is giving correct date but not the correct day value.Why..?

它给出了正确的日期,但不是正确的日期值。为什么......?

What I want day is the date for monday for the current week which can be generated on any day of the week. so what I did was, I'm taking the today's day and comparing with (Mon,Tue.... Sun) and respectively creating a timestamp using

我想要的日期是本周的星期一日期,可以在一周中的任何一天生成。所以我所做的是,我将今天的一天与 (Mon,Tue .... Sun) 进行比较,并分别使用

case "Mon":

$startdate1=date("d-m-y");
$parts = explode('-',$startdate1);
$startdate2 = date('d-m-Y',mktime(0,0,0,$parts[1],($parts[0]+1),$parts[2]));
$startdate3 = date('d-m-Y',mktime(0,0,0,$parts[1],($parts[0]+2),$parts[2]));
$startdate4 = date('d-m-Y',mktime(0,0,0,$parts[1],($parts[0]+3),$parts[2]));
$startdate5 = date('d-m-Y',mktime(0,0,0,$parts[1],($parts[0]+4),$parts[2])); 
$startdate6 = date('d-m-Y',mktime(0,0,0,$parts[1],($parts[0]+5),$parts[2]));
$startdate7 = date('d-m-Y',mktime(0,0,0,$parts[1],($parts[0]+6),$parts[2]));

$dates=array(1 => $startdate1,$startdate2,$startdate3,$startdate4,$startdate5,$startdate6,$startdate7);
$i=1;
while( $i <= 7 )
{
echo $dates[$i];
$i++;
}
break;

$date is the final array respective to today that has to be returned. Is there any other better method to do this operation.

$date 是必须返回的与今天相关的最终数组。有没有其他更好的方法来做这个操作。

采纳答案by anurag-jain

What i did to resolve it is used the date format ('d-m-Y') instead of ('d-m-y') in date function, which was causing the problem. Hence strtotime accepted the format and gave the correct result for

我为解决它所做的是在日期函数中使用日期格式 ('dm-Y') 而不是 ('dm-y'),这导致了问题。因此 strtotime 接受了格式并给出了正确的结果

$t=date('d-m-Y');
echo date("D",strtotime($t));

回答by chanchal

I tried this to get current day.

我试过这个来获得当前日期。

echo date('l'); // output: current day.

回答by David Snabel-Caunt

How about this:

这个怎么样:

//today is monday
if (1 == date('N')){
    $monday = time();
}else{
    $monday = strtotime('last Monday');
}

for ($i = 0; $i < 7; $i++){
    echo date('d-m-Y', $monday) . '<br>';
    $monday = strtotime('tomorrow', $monday);
}

First find Monday, if it is not today, then print 7 dates

先找星期一,如果不是今天,则打印7个日期

回答by codeholic

What I want day is the date for monday for the current week which can be generated on any day of the week.

我想要的日期是本周的星期一日期,可以在一周中的任何一天生成。

That's what you want. $mdayis the month day of this week's Monday. Nevermind if it's not positive, mktimewill handle that right. $mondayhas the timestamp of the Monday's midnight.

那就是你想要的。$mday是本周星期一的月日。没关系,如果它不是积极的,mktime会处理好。$monday具有星期一午夜的时间戳。

$now = getdate();
$mday = $now['mday'] - ($now['wday'] + 6) % 7;
$monday = mktime(0, 0, 0, $now['mon'], $mday, $now['year']);
echo(date('d-m-y', $monday));

回答by Mustafa ELnagar

I use the function date and path to it the "D" that refere to the current day , and it works with me

我使用函数日期和指向它的路径“D”,它指的是当天,它与我一起工作

 $today = date("D"); 

and to get the full info about the current date

并获取有关当前日期的完整信息

  $today = date("D M j G:i:s T Y");               // Sat Mar 10 17:16:18 MST 2001

回答by poke

what i tried is here...

echo date("D",$x)."<br>";

我试过的就在这里...

echo date("D",$x)."<br>";

dateexpects a timestamp (int) value as the second parameter. Your $xis a string containing an ambiguous date format. Convert that date into a timestamp first, using strptimeor strtotimeand use the datefunction correctly to get the correct day value.

date期望时间戳 (int) 值作为第二个参数。您$x是一个包含不明确日期格式的字符串。首先将该日期转换为时间戳,使用strptimeorstrtotimedate正确使用该函数以获得正确的日期值。

Regarding your second part, you don't need to (and shouldn't) check the day name to calculate the correct Monday, Tuesday etc. A more efficient approach is for exampleusing strtotimeto get last Mondayetc.

关于您的第二部分,您不需要(也不应该)检查日期名称来计算正确的星期一、星期二等。更有效的方法是例如使用strtotimegetlast Monday等。

回答by Gordon

You are likely passing a string as timestamp

您可能将字符串作为时间戳传递

echo $x."<br>";
echo date("D",$x)."<br>";

Remove $xand it will output the correct day or change it to

删除$x,它将输出正确的日期或将其更改为

$x = '21-02-2010';
echo date('D', strtotime($x));