WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP 5 Date and Time


The PHP date() work is utilized to organize a date or potentially a time.


The PHP Date() Function

The PHP date() work arranges a timestamp to an increasingly discernible date and time.

Syntax

date(format,timestamp)
Parameter Description
format Required. Determines the arrangement of the timestamp
timestamp Optional. Determines a timestamp. Default is the present date and time

Get a Simple Date

The required format parameter of the date() work determines how to arrange the date (or then again time).

Here are a few characters that are usually utilized 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 likewise be embedded between the characters to include extra formatting.

The model underneath arrangements the present date in three distinctive ways:

Example

<?php
echo "Today is " . date("Y/m/d") . "<br>";
echo "Today is " . date("Y.m.d") . "<br>";
echo "Today is " . date("Y-m-d") . "<br>";
reverberation "Today is " . date("5");
?>
Run precedent »

PHP Tip - Automatic Copyright Year

Use the date() capacity to naturally refresh the copyright year on your website:

Example

&copy; 2018-<?php reverberation date("Y");?>
Run precedent »

Get a Simple Time

Here are a few characters that are regularly utilized for times:

  • h - 12-hour organization of a hour with driving zeros (01 to 12)
  • i - Minutes with driving zeros (00 to 59)
  • s - Seconds with driving zeros (00 to 59)
  • a - Lowercase Ante meridiem and Post meridiem (am or pm)

The precedent underneath yields the present time in the predetermined format:

Example

<?php
echo "The time is " . date("h:i:sa");
?>
Run precedent »

Get Your Time Zone

If the time you returned from the code isn't the privilege time, it's likely in light of the fact that your server is in another nation or set up for an alternate timezone.

So, on the off chance that you need an opportunity to be right as indicated by a particular area, you can set a timezone to utilize.

The model beneath sets the timezone to "India", at that point yields the present time in the predefined format:

Example

<?php
date_default_timezone_set("India");
echo "The time is " . date("h:i:sa");
?>
Run model »

Create a Date With PHP mktime()

The discretionary timestamp parameter in the date() work indicates a timestamp. On the off chance that you don't indicate a timestamp, the present date and time will be utilized (as appeared in the models above).

The mktime() work restores the Unix timestamp for a date. The Unix timestamp contains the quantity 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 precedent underneath makes a date and time from various parameters in the mktime() function:

Example

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

Create a Date From a String With PHP strtotime()

The PHP strtotime() work is utilized to change over an intelligible string to a Unix time.

Syntax

strtotime(time,now)

The model beneath makes a date and time from the strtotime() function:

Example

<?php
$d=strtotime("2:12pm june 14 2017");
echo "Created date is " . date("Y-m-d h:i:sa", $d);
?>
Run model »

PHP is very astute about changing over a string to a date, so you can put in different values:

Example

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

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

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

However, strtotime() isn't immaculate, so make sure to check the strings you put in there.


More Date Examples

The precedent underneath yields the dates for the following six Saturdays:

Example

<?php
$startdate = strtotime("Mondayquot;);
$enddate = strtotime("+2 weeks",$startdate);

while ($startdate < $enddate) {
  reverberation date("M d", $startdate),"<br>";
  $startdate = strtotime("+3 week", $startdate);
}
?>
Run precedent »

The precedent underneath yields the quantity of days until 5 of August :

Example

<?php
$d1=strtotime("August 05");
$d2=ceil(($d1-time())/60/60/24);
reverberation "There are " . $d2 ." days until fifth of August.";
?>
Run precedent »