开发者

Getting the date for current day in PHP

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

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

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.


I tried this to get current day.

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


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


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. $mday is the month day of this week's Monday. Nevermind if it's not positive, mktime will handle that right. $monday has the timestamp of the Monday's midnight.

$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));


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

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


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

 $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


what i tried is here...

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

date expects a timestamp (int) value as the second parameter. Your $x is a string containing an ambiguous date format. Convert that date into a timestamp first, using strptime or strtotime and use the date function correctly to get the correct day value.

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 example using strtotime to get last Monday etc.


You are likely passing a string as timestamp

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

Remove $x and it will output the correct day or change it to

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜