Date and Time in PHP with Examples [2 Steps]

In this article we are going to cover Date and Time in php with Examples.

What is date and time in PHP?

Time and date functions in PHP informs about current date and time. Date and time are some of the most frequently used operations in PHP while executing SQL queries or designing a website. Time and date can be manipulated by using these function.

Get a Date in PHP:

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

Example of simple get date in PHP:

<!DOCTYPE html>
<html>
<body>
<?php
echo "Today's date is :";
$today = date("d/m/Y");
echo $today;
  ?>
</body>
</html>

Output:

Today's date is :09/07/2021
Date and Time in PHP with Examples [2 Steps] 1

Formatting options available in date() function in PHP:

The format parameter of the date() function is a string that can contain multiple character  allowing to generate dates in various formats.

Here are some characters that are commonly used for dates:

d- represents day of the month, two digits with leading zeros(01 or 31).

D- represents day of the week in text as an abbreviation (Mon to sun).

m- represents months in numbers with leading zeros (01 or 12).

M- represents months in text, abbreviated (Jan to Dec).

y- represents year in two digit(e.g, 09 or 19) Y- represents year in four digit (e.g, 2005 or 2020)

Example of date() function in PHP:

<!DOCTYPE html>
<html>
<body>

<?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>";
echo "Today is " . date("D");
?>

</body>
</html>

Output:

Today is 2021/07/09
Today is 2021.07.09
Today is 2021-07-09
Today is Fri
Date and Time in PHP with Examples [2 Steps] 2

PHP Tip – automatic Copyright Year:

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

Example of PHP Tip:

<!DOCTYPE html>
<html>
<body>

© 2015-<?php echo date("Y");?>

</body>
</html> 

Output:

© 2015-2021
Date and Time in PHP with Examples [2 Steps] 3

Get a Time in PHP:

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)

Example of time() function in PHP:

<!DOCTYPE html>
<html>
<body>

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

</body>
</html>

Output:

The time is 08:03:53pm
Date and Time in PHP with Examples [2 Steps] 4

Get Your Time Zone in PHP:

If the time you got back from the code is not correct, its 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.

Example of  Get Your Time Zone in PHP:

<!DOCTYPE html>
<html>
<body>

<?php
date_default_timezone_set("asia/kolkata");
echo "The time is " . date("h:i:sa");
?>

</body>
</html>

Output:

The time is 11:47:46pm
Date and Time in PHP with Examples [2 Steps] 5

Create a Date With mktime() function in PHP:

The mktime() function returns the Unix timestamp for a date. The Unix timestamp contains the number of second between the Unix Epoch (January 1, 1970, 00:00:00 GMT) and the time specified. The hour, minute, second, month, day and year are sent as parameters to the mktime() function and it returns an integer Unix timestamp on success and False on error.

Syntax:

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

Parameters: This function accepts seven parameter as mentioned above and describe below:

$hour: it is an optional parameter which specifies the hour.

$minute: it is an optional parameter which specifies the minute.

$second: it is an optional parameter which specifies the second.

$month: it is an optional parameter which specifies the month.

$day: it is an optional parameter which specifies the day.

$year: it is an optional parameter which specifies the year.

Example of Create a Date With mktime() function in PHP:

<!DOCTYPE html>
<html>
<body>

<?php
$d=mktime(12, 23, 58, 06, 21, 2021);
echo "Created date is " . date("Y-m-d h:i:sa", $d);
?>

</body>
</html>

Output:

Created date is 2021-06-21 12:23:58pm
Date and Time in PHP with Examples [2 Steps] 6

Create a Date From a String With strtotime() in PHP

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

Syntax:

Strtotime(time, now)

Example of Create a Date From a String With strtotime()

<!DOCTYPE html>
<html>
<body>

<?php
$d=strtotime("06:37am June 26 2020");
echo "Created date is " . date("Y-m-d h:i:sa", $d);
?>

</body>
</html>

Output:

Created date is 2020-06-26 06:37:00am
Date and Time in PHP with Examples [2 Steps] 7

Another Example of Create a Date From a String With strtotime() in PHP:

<!DOCTYPE html>
<html>
<body>

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

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

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

</body>
</html>

Output:

2021-07-12 12:00:00am
2021-07-14 12:00:00am
2021-09-11 09:53:58am
Date and Time in PHP with Examples [2 Steps] 8

Date Examples in PHP:

<!DOCTYPE html>
<html>
<body>

<?php
$startdate=strtotime("Monday");
$enddate=strtotime("+6 weeks", $startdate);

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

</body>
</html>

Output:

Jul 12
Jul 19
Jul 26
Aug 02
Aug 09
Aug 16
Date and Time in PHP with Examples [2 Steps] 9

Conclusion:

In this article we have covered Date and Time in php with Examples.

Related Articles:

Introduction about PHP [PHP 7]

Reference:

php date and time official page

Shweta Mamidwar

I am Shweta Mamidwar working as a Intern in Product Company. Likes to share knowledge.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap