Get current day in php

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

echo $x."
"; echo date("D",$x)."
";

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.

Topic: PHP / MySQLPrev|Next

Answer: Use the PHP date() Function

You can simply use the PHP date() function to get the current data and time in various format, for example, date('d-m-y h:i:s'), date('d/m/y H:i:s'), and so on.

Try out the following example to see how it basically works:

The date and time returned by the above example is based on the server's default timezone setting. If you want to show date and time as per user's timezone, you can set the timezone manually using the date_default_timezone_set() function before the date() function call.

The following example shows the date and time in India timezone which is Asia/Kolkata.

To learn more about date and time handling, please check out the PHP date and time tutorial.

For a complete list of timezones supported by PHP, see timezone reference.


Here are some more FAQ related to this topic:

  • How to extract substring from a string in PHP
  • How to split a string into an array in PHP
  • How to get the current year using PHP


The PHP date() function is used to format a date and/or a time.


The PHP Date() Function

The PHP date() function formats a timestamp to a more readable date and time.

Syntax

ParameterDescription
format Required. Specifies the format of the timestamp
timestamp Optional. Specifies a timestamp. Default is the current date and time

A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred.


Get a Date

The required format parameter of the date() function specifies how to format the date (or time).

Here are some characters that are commonly used for dates:

  • d - Represents the day of the month (01 to 31)
  • m - Represents a month (01 to 12)
  • Y - Represents a year (in four digits)
  • l (lowercase 'L') - Represents the day of the week

Other characters, like"/", ".", or "-" can also be inserted between the characters to add additional formatting.

The example below formats today's date in three different ways:

Example

echo "Today is " . date("Y/m/d") . "
";
echo "Today is " . date("Y.m.d") . "
";
echo "Today is " . date("Y-m-d") . "
";
echo "Today is " . date("l");
?>

Try it Yourself »



Use the date() function to automatically update the copyright year on your website:


Get a Time

Here are some characters that are commonly used for times:

  • H - 24-hour format of an hour (00 to 23)
  • h - 12-hour format of an hour with leading zeros (01 to 12)
  • i - Minutes with leading zeros (00 to 59)
  • s - Seconds with leading zeros (00 to 59)
  • a - Lowercase Ante meridiem and Post meridiem (am or pm)

The example below outputs the current time in the specified format:

Note that the PHP date() function will return the current date/time of the server!


Get Your Time Zone

If the time you got back from the code is not correct, it's probably because your server is in another country or set up for a different timezone.

So, if you need the time to be correct according to a specific location, you can set the timezone you want to use.

The example below sets the timezone to "America/New_York", then outputs the current time in the specified format:

Example

date_default_timezone_set("America/New_York");
echo "The time is " . date("h:i:sa");
?>

Try it Yourself »


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. The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.

Syntax

mktime(hour, minute, second, month, day, year)

The example below creates a date and time with the date() function from a number of parameters in the mktime() function:

Example

$d=mktime(11, 14, 54, 8, 12, 2014);
echo "Created date is " . date("Y-m-d h:i:sa", $d);
?>

Try it Yourself »


Create a Date From a String With strtotime()

The PHP strtotime() function is used to convert a human readable date string into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).

Syntax

The example below creates a date and time from the strtotime() function:

Example

$d=strtotime("10:30pm April 15 2014");
echo "Created date is " . date("Y-m-d h:i:sa", $d);
?>

Try it Yourself »

PHP is quite clever about converting a string to a date, so you can put in various values:

Example

$d=strtotime("tomorrow");
echo date("Y-m-d h:i:sa", $d) . "
";

$d=strtotime("next Saturday");
echo date("Y-m-d h:i:sa", $d) . "
";

$d=strtotime("+3 Months");
echo date("Y-m-d h:i:sa", $d) . "
";
?>

Try it Yourself »

However, strtotime() is not perfect, so remember to check the strings you put in there.


More Date Examples

The example below outputs the dates for the next six Saturdays:

Example

$startdate = strtotime("Saturday");
$enddate = strtotime("+6 weeks", $startdate);

while ($startdate < $enddate) {
  echo date("M d", $startdate) . "
";
  $startdate = strtotime("+1 week", $startdate);
}
?>

Try it Yourself »

The example below outputs the number of days until 4th of July:

Example

$d1=strtotime("July 04");
$d2=ceil(($d1-time())/60/60/24);
echo "There are " . $d2 ." days until 4th of July.";
?>

Try it Yourself »


Complete PHP Date Reference

For a complete reference of all date functions, go to our complete PHP Date Reference.

The reference contains a brief description, and examples of use, for each function!


PHP Exercises



How can I get today's day in PHP?

You can simply use the PHP date() function to get the current data and time in various format, for example, date('d-m-y h:i:s') , date('d/m/y H:i:s') , and so on.

How do you get the day of the week from a date in PHP?

Use strtotime() function to get the first day of week using PHP. This function returns the default time variable timestamp and then use date() function to convert timestamp date into understandable date.

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 can I get current Indian time in PHP?

In PHP, set your desired timezone, then just print the date: date_default_timezone_set('Asia/Kolkata'); echo date('d-m-Y H:i');.
This also outputs system time only. ... .
If your system is in India and you set the timezone to Kolkata, then yes, it'll give you the system time..