Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 217
anurag-jain Avatar asked Feb 21 '10 16:02

anurag-jain


People also ask

What does date () do in PHP?

The date/time functions allow you to get the date and time from the server where your PHP script runs. You can then use the date/time functions to format the date and time in several ways. Note: These functions depend on the locale settings of your server.

How can I get current date in dd mm yyyy format in PHP?

$today = date('d-m-y'); to $today = date('dd-mm-yyyy');

How get fetch date from database in PHP?

Create a Date With mktime() The optional timestamp parameter in the date() function specifies a timestamp. If omitted, the current date and time will be used (as in the examples above). The PHP mktime() function returns the Unix timestamp for a date.

How can I get tomorrow date in PHP?

$newDate = date('Y-m-d', strtotime('tomorrow')); echo $newDate; ?>


2 Answers

I tried this to get current day.

echo date('l'); // output: current day.
like image 117
chanchal Avatar answered Oct 29 '22 18:10

chanchal


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

like image 34
David Snabel-Caunt Avatar answered Oct 29 '22 17:10

David Snabel-Caunt