views:

407

answers:

5

I want to get the date for current date 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.

A: 

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));
Gordon
Still the output is Thu for $x = '21-02-2010';
Anurag
yeah got the right one this time... :) Thanks
Anurag
+3  A: 

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.

poke
echo date("D",strtotime($x,1))."<br>";this is also not giving correct answer.
Anurag
yeah got the right one this time... :) Thanks
Anurag
+1  A: 

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

David Caunt
if (1 == date('N')) what is 'N' in this line... can u pls explain me the code a little more...?
Anurag
N is the numeric representation for the day of the week. See http://uk2.php.net/manual/en/function.date.php
David Caunt
+2  A: 

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));
codeholic
Seems like hard work to find last Monday :)
David Caunt
Moreover it could never exist: http://www.last-thursday.org/
codeholic
A: 

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