php

How to Get the Current Date and Time in PHP

If you are developing websites or you are working on them, you will often need to work with dates and time in PHP.

For example, you may want to find the last changed date on any post or if you want to mention how long ago a user wrote any comment.

You might additionally have to display a countdown of the days until a particular event.

Fortunately, PHP comes with amazing built-in date and time functions which can help us to accomplish all that much more easily.

In this article, you will be learning how to get the current date and time in PHP programming language.

This includes very easy steps and the functions employed for this purpose are very simple to implement.

Functions that are used to get the current date and time in PHP are listed below:

date() function

getdate() function

PHP assists us with both these predefined functions for this task. Both these functions can be utilized to provide the current date and time, though they have different execution processes and syntax.

 

Also, Read – Introduction to Bootstrap 4 Flex Layout (Flexbox for Bootstrap)

 

PHP date() Function

The PHP date() function is an in-built function that simplifies working with data types related to date formats. It simply changes the timestamp into a readable date and time format.

It is used to represent the date of an article when it was published or record the last updated data in a database.

Why We Need This Date() Function?

Each computer stores time and date in a format named UNIX Timestamp, which measures time as the number of seconds since the beginning of the Unix time i.e. midnight Greenwich Mean Time.

As this is an impractical form for humans to understand and learn, PHP makes this timestamp readable and more understandable to humans by converting it into a proper format.

PHP – Overview

PHP stands for hypertext pre-processor which is a server-side scripting language.

It is widely used in developing and strengthening dynamic websites, or static websites, web development, and applications across all domains. PHP scripts can only be executed on any server that has PHP installed.

It is an open-source language and is free of cost, and is often downloaded everywhere. It is readily available to use for an event of web applications.

Here, applications can easily be loaded which are based on PHP and connected to the database. It has a powerful library to use multiple function modules for data representation.

PHP is flexible which makes it ready to efficiently combine with other programming languages.

How to Get the Current Date and Time in PHP

Use the PHP date() Function

You can use the PHP date() function to get the current time and date in various format, for example, date(‘d-m-y h:i:s’), date(‘d/m/y H:i:s’), and more.

Try out the following example to see how it works:

Examples:

Using Date() Function

Date() function formats timestamps into readable date and time.

date(format, timestamp)

The example below example will show date time in (‘d-m-y h:i:s’) format:

<?php

// Return current date and time from the server

$currentDateTime = date('d-m-y h:i:s');

echo $currentDateTime ;

?>

The above example returns date and time based on the server’s default timezone setting.

You can have a more clear understanding by this example below, which displays the date and time in India’s timezone i.e. Asia/Kolkata.

Example 1:

 <?php

// Now Setting the new timezone

date_default_timezone_set('Asia/Kolkata');

$date = date('d-m-y h:i:s');

echo $date;

?>

To Change the Different Place:

Example 2:

<?php

date_default_timezone_set('Europe/Amsterdam');    

$DateAndTime = date('m-d-Y h:i:s a', time());  

echo "The current date and time in Amsterdam are $DateAndTime.\n";

date_default_timezone_set('America/Toronto');    

$DateAndTime2 = date('m-d-Y h:i:s a', time());  

echo "The current date and time in Toronto are $DateAndTime2.";

?>

Also, Read – 7 Best Reasons to Choose React Native for Mobile App Development

Final Thoughts

The above section explained how to get the current date and time in PHP desired format using the date() function.

We also saw that date() can also be used to get the current time and date of different places as desired by different users.

Here we have tried to cover the key DateTime methods and functions. You should take a look at the examples defined above to get a complete understanding of how to use these functions. Start with your coding today and have fun!