PHP date() and timezone ? – viralscripts

Spread the love with your friends

PHP date formats and time zone
PHP date formats and time zone

Hello friends, How are you all. Dear guys today in this post we will learn some date and time related issue in PHP. Most of freshers and experienced developers also search these concepts on internet and confuse in all these things.

But today you will learn all date and time related issue here.

So first of all i will share the content of the topics that we will learn today in PHP.

Table of content :

  1. How to get date and time in PHP ?

  2. How to set Time zone in PHP ? (Your country wise)

So guys these all the topic which we are going to discuss today.

So guys lets start the topic with demonstrate each concept with the help of examples.

How to get date and time in PHP ?

First of we will know how to get date and time in PHP.

Code :

<!DOCTYPE html>
<html>
<body>
<?php
echo “Today Date with /  ” . date(“Y/m/d”) . “<br>”;
echo “Today Time with . ” . date(“Y.m.d”) . “<br>”;
echo “Today Time with – ” . date(“Y-m-d”) . “<br>”;
// Lets understand now the code of time in PHP
echo “Time in format of : ” . date(“h:i:s”) . “<br>”;
echo “Time in format of -” . date(“h-i-s”) . “<br>”;
echo “Time in format of /” . date(“h/i/s”) . “<br>”;
// Lets understand the AM and PM in time
echo “Time in format of :” . date(“h:i:s A”) . “<br>”;
// here small a represent am/pm in small letters
// and a capital letter of A – Represents AM/PM in capital Letter
?>
</body>
</html>

Result :

Today Date with / 2022/03/10
Today Time with . 2022.03.10
Today Time with – 2022-03-10
Time in format of : 02:00:36
Time in format of -02-00-36
Time in format of /02/00/36
Time in format of :02:00:36 PM

So guys the above example of date and time represent how to display time and date in PHP. We also here represent many formats of day and time.

  1. How to set Time zone in PHP ?

This date and time came as per default time zone. If you want to set time zone as per India and for your country then before the time you need to set Time zone and then the time will show as per your clock time.

You can set the time zone like this :

<!DOCTYPE html>
<html>
<body>
<?php
date_default_timezone_set(“Asia/Calcutta”);
echo “Today Date with /  ” . date(“Y/m/d”) . “<br>”;
echo “Today Time with . ” . date(“Y.m.d”) . “<br>”;
?>
</body>
</html>

So guys today you learn two new topic in PHP. How to set time zone and how to display date and time in PHP.

Dear friends, If you have some suggestion and any point about this post and want to share some more details then write down in comment box. Thanks.


Spread the love with your friends

Leave a Comment

PHP Code Snippets Powered By : XYZScripts.com