PHP Get Date and Time

 

PHP Date And Time



To fetch the current date and time we can use inbuilt php date() function.

Syntax

 
<?php

date(string format, int [timestamp]);

?>

Kindly note that this date() function will only print date and time of a local system date (Local PC) or server system date (Server PC Date).

To check the server pc date or local pc date use this function "phpinfo()".

 
<?php

phpinfo();

?>

Available Date Format Characters


Days

  • d : will print days of month i.e 01 to 31.
  • j : will print days of month with out leading 0 before single digit number i.e. 1 to 31
  • D : will print name of day in three letters i.e. MON, TUE, WED..Sat
  • l : will print name of day i.e Monday, Tuesday..
  • S : will print English ordinal suffix i.e st, nd, rd or th.

Months

  • F : will print name of month like January, Febuary..December.
  • m: will print month in numeric value i.e. 01 to 12.
  • M : will print month in 3 letters i.e. Jan, Feb,..Dec.

Year

  • Y : will print year in standard format i.e 1999...2999.
  • y : will print last two number of a year i.e. 99....13 (1999...2013).

Time

  • h : will print 12-hour fomat i.e 01:00 ....12:59
  • H : will print 24-hour format i.e 00:00...23:59
  • i : will print minutes i.e 01:00 to 01:59
  • s : will print seconds i.e 01:00:00 to 01:00:59
  • A : will print uppercase "AM" and "PM"
  • a : will print lowercase "am" and "pm"

By using these different types of formats you can print date and time in different ways as per your need.

Example of Data and Time

 
<?php

// 2013-04-30
echo date("Y-m-d")."<br>";

//2013-04-30 08:32:18
echo date("Y-m-d H:i:s")."<br>";

//Tuesday,April 30, 2013 08:32:18 am
echo date('l,F d, Y h:i:s a')."<br>";

//Tuesday, April 30, 2013, 8:32:18 AM
echo date("l, F j, Y, g:i:s A")."<br>";

//08:32:18 AM
echo date("H:i:s A")."<br>";

//Tuesday, April 30th, 2013, 8:39:14 AM
echo date("l, F jS, Y, g:i:s A")."<br>";

//Tuesday, April 30th, 2013
echo date("l, F jS, Y")."<br>";

//30th-April-2013
echo date("jS-F-Y")."<br>";

//Tue,30th-April-2013
echo date("D,jS-F-Y")."<br>";

//Tue,30-April-2013
echo date("D,d-F-Y")."<br>";

//Tue,2013-04-30
echo date("D,Y-m-d")."<br>";
?>

Timestamp in php

mktime() is a php function which helps to create unix timestamp. mktime takes following argument hour, minutes, second, month, day, year.

For Example

 
<?php
// May 01, 2013 was on a Wednesday
echo "May 01, 2013 was on a ".date("l", mktime(0,0,0,05,01,2013));
?>

Use of mktime() function

mktime() function is very uselful to get

  • 1. Name of day on that Date
  • 2. To retrive previous back date or previous 6 months or 8 months and so on date from current date
  • 3. To retrive upcoming 8 months, 6 months, 1 year and so on date from current date

Examples are using mktime() function are shown below

 
<?php
// May 01, 2013 was on a Wednesday
echo "May 01, 2013 was on a ".date("l", mktime(0,0,0,05,01,2013))."<br>";

//May-01-2013
echo date("M-d-Y", mktime(1, 2, 3, 05, 01, 2013))."<br>";

$THREEmonthsdate  = mktime(0, 0, 0, date("m")  , date("d")+90, date("Y"));

//Tuesday, Jul-30-2013
echo date("l, M-d-Y", $THREEmonthsdate)."<br>";

$previousSIXmonthsdate  = mktime(0, 0, 0, date("m")  , date("d")-180, date("Y"));

//Friday, Nov-02-2012
echo date("l, M-d-Y", $previousSIXmonthsdate)."<br>";
?>

Fetching Upcoming 3 months date from current date

 
<?php
$THREEmonthsdate  = mktime(0, 0, 0, date("m")  , date("d")+90, date("Y"));

//Tuesday, Jul-30-2013
echo date("l, M-d-Y", $THREEmonthsdate)."<br>";
?>

Fetching Previous 6 months date from current date

 
<?php
$previousSIXmonthsdate  = mktime(0, 0, 0, date("m")  , date("d")-180, date("Y"));

//Friday, Nov-02-2012
echo date("l, M-d-Y", $previousSIXmonthsdate)."<br>";
?>

Timezone using PHP


date_default_timezone_get() and date_default_timezone_set() functions helps to set and get timezone using PHP date_default_timezone_set() Function

By using this function you can set the time zone according to your city/country.Example is shown below.

 
<?php
date_default_timezone_set("Asia/Calcutta");
?>

List of supported timezone available from official php website. http://www.php.net/manual/en/timezones.asia.php

date_default_timezone_get() Function

This function helps to retrive the date and time according to current timezone.

 
<?php
date_default_timezone_get();
// prints something like: Wednesday the 1st
echo date('l \t\h\e jS');
?>

So hey friends if you have aby doubts or question regarding PHP date and time. kindly mail me or add your comment.

 

 

:: SHARE THIS POST ::

        
 

More PHP Articles To Read Next:

 
Apr
28
2013

What is PHP with a simple example using PHP

CLICK TO READ FULL ARTICLE

Views - 11743 |Category - PHP

 
Author: Gurunatha Dogi
is a software engineer by profession and founder of Onlinebuff.com, Onlinebuff is a tech blog which covers topics on .NET Fundamentals, Csharp, Asp.Net, PHP, MYSQL, SQL Server, Cell Phones and lots more. Follow me @ and twitter.
Leave a Comment......

All fields marked with * are mandatory

bh1c8

 

 
Comments

Posted by Prabhakaran on 2015-10-19

how to know current system date and time using php codes or html

Posted by Anil on 2015-08-05

how to store the date in database.