PHP Date and Time

With the PHP Date and Time function date(), you can change the way a date or time looks.

The PHP Date() Function

Date() is a function in PHP. With the PHP date() function, a timestamp is turned into a date and time that is easier to read.

Syntax

date(format,timestamp)

Parameter Description
format Required.
Indicates how the timestamp should be written.
timestamp Optional.
Specifies a timestamp. The current date and time are used by default.

Format for Parameter Description

Required. Indicates how the timestamp should be written. Optional. Specifies a timestamp. The current date and time are used by default.

A timestamp is a string of characters that shows the date and/or time when something happened.

Get a Date

The date() function needs a parameter called format to tell it how to format the date (or time).

Here are some dates that often start with these letters:

  • d: Indicates the date of the month (01 to 31)
  • m stands for a month (01 to 12)
  • Y stands for the year (in four digits)
  • l (small letter \’L\’) – Shows what day of the week it is

You can also put other characters like \”/\”, \”.\” or \”-\” between the characters to add more formatting.

In the following example, the date of today is shown in three different ways:

<!DOCTYPE html>
<html>
<body>

<?php
echo \”Date is \” . date(\”Y/m/d\”) . \”<br>\”;
echo \”Date is \” . date(\”Y.m.d\”) . \”<br>\”;
echo \”Date is \” . date(\”Y-m-d\”) . \”<br>\”;
echo \”Today is \” . date(\”l\”);
?>

</body>
</html>

Automatic Copyright Year with PHP date

 

<!DOCTYPE html>
<html>
<body>

© copyright 2020-<?php echo date(\”Y\”);?>

</body>
</html>

Get a Time

Here are some commonly used characters for times:

H – An hour in the 24-hour format (00 to 23)
h – Format for a 12-hour hour with leading zeros (01 to 12)
I – Minutes that start with a zero (00 to 59)
s – Seconds with zeros in front (00 to 59)
a: Put Ante meridiem and Post meridiem in lowercase (am or pm)

The following example shows how to show the current time:

Example

 

<?php date(\”h:i:sa\”);?>

 

<!DOCTYPE html>
<html>
<body>

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

</body>
</html>

Keep in mind that the PHP date() function will return the server\’s current date and time!

Get Your Time Zone

If the time you got from the code is wrong, it\’s probably because your server is in a different country or is set up for a different time zone.

So, you can set the timezone you want to use if you need the time to be right for a certain place.

The following example sets the timezone to \”Asia/Calcutta\” and then shows the current time in the format you choose:

<!DOCTYPE html>
<html>
<body>

<?php
date_default_timezone_set(\”Asia/Calcutta\”);
echo \”The time is \” . date(\”h:i:sa\”);
?>

</body>
</html>

 

With mktime, you can create a date ()

The timestamp parameter, which is optional, tells the date() function what time it is. If you don\’t include this, the current date and time will be used (as in the examples above).

A date\’s Unix timestamp is what the PHP mktime() function gives back. The Unix timestamp shows the number of seconds between the Unix Epoch (January 1, 1970, 00:00:00 GMT) and the time given.

Syntax

 

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

In the example below, a number of parameters in the mktime() function are used to create a date and time with the date() function:

<!DOCTYPE html>
<html>
<body>

<?php
$date=mktime(10, 25, 30, 1, 10, 2023);
echo \”Created date is \” . date(\”Y-m-d h:i:sa\”, $date);
?>

</body>
</html>

Related Posts
50+ PHP Interview Questions and Answers 2023

1. Differentiate between static and dynamic websites. Static Website The content cannot be modified after the script is executed The Read more

All We Need to Know About PHP Ecommerce Development

  Many e-commerce sites let you search for products, show them off, and sell them online. The flood of money Read more

PHP Custom Web Development: How It Can Be Used, What Its Pros and Cons Are,

PHP is a scripting language that runs on the server. It uses server resources to process outputs. It is a Read more

PHP Tutorial

Hypertext Preprocessor (PHP) is a programming language that lets web developers make dynamic content that works with databases. PHP is Read more

Introduction of PHP

PHP started out as a small open source project that grew as more and more people found out how useful Read more

Syntax Overview of PHP

This chapter will show you some of PHP\'s very basic syntax, which is very important for building a strong PHP Read more

Environment Setup in PHP

To develop and run PHP on your computer, you need to instal three important parts. Web server PHP can almost Read more

Variable Types in PHP

Using a variable is the main way to store information in the middle of a PHP program. Here are the Read more

Scroll to Top