php

How to get the current date and time in PHP

In this tutorial, we are going to see how to get the current date and time in PHP. You can use PHP’s date() function to get the current date and time in different formats, for example, date('d-m-y h:i:s'), date('d/m/y H:i:s'), etc.
 

How to get the current date and time in PHP
<?php
	$date = date('d-m-y h:i:s');
	echo $date;
?>

Output:

09-07-19 02:28:14

The date and time returned by the example above are based on the server’s default time zone setting. If you want to display the date and time according to the user’s time zone, you can set it manually using the date_default_timezone_set() function before calling the date() function.
 

 
The following example displays date and time in America, in the time zone corresponding to America/Los_Angeles.

<?php
	// Define the new time zone
	date_default_timezone_set('America/Los_Angeles');
	$date = date('d-m-y h:i:s');
	echo $date;
?>

Output:

09-07-19 08:24:38
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *