Asked By
amscott
20 points
N/A
Posted on - 04/04/2012
Hi there.
Ive been practicing php and html programming these past few weeks.
I am now currently working on some website and I just want the codes for getting the time and date.
What is the code for getting the current time and date of the viewer?
Thank you.
Php Code for getting the current time and date
PHP has the ability to generate the date and time dynamically . Here I added few sample of date and time script:
Script- 1 :
<?php
// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('UTC');
// Prints something like: Monday
echo date("l");
// Prints something like: Sunday 10th of February 2012 03:16:23 AM
echo date('l jS of F Y h:i:s A');
// Prints: January 10, 2012 is on a Monday
echo "January 10, 2012 is on a " . date("l", mktime(0, 0, 0, 7, 1, 200));
/* use the constants in the format parameter */
// prints something like: Mon, 15 Aug 2005 15:12:46 UTC
echo date(DATE_RFC822);
// prints something like: 2000-07-01T00:00:00+00:00
echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2012));
?>
Script- 2 :
<?php
// prints something like: Wednesday the 15th
echo date('l the jS');
?>
Script- 3 :
<?php
// Assuming today is May 15th, 2012, 5:16:18 pm, and that we are in the
// Mountain Standard Time (MST) Time Zone
$today = date("F j, Y, g:i a");Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // January 15, 2012, 2:10 am
$today = date("m.d.y");Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // 15.01.12
$today = date("j, n, Y");Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // 1, 3, 2001
$today = date("Ymd");Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // 20120115
$today = date('h-i-s, j-m-y, it is w Day');Â Â Â Â // 05-16-18, 15-01-12, 1631 1618 6 Satpm01
$today = date('it is the jS day.');Â Â // it is the 15th day.
$today = date("D M j G:i:s T Y");Â Â Â Â Â Â Â Â Â Â Â Â Â Â // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s m is month');Â Â Â Â // 17:03:18 m is month
$today = date("H:i:s");Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // 17:16:18
?>
Â
Script- 4 :
<?php
 $b = time ();
 print date("m/d/y",$b) . "<br>";
 print date("D, F jS",$b) . "<br>";
 print date("l, F jS Y",$b) . "<br>";
 print date("g:i A",$b) . "<br>";
 print date("r",$b) . "<br>";
 print date("g:i:s A D, F jS Y",$b) . "<br>";
 ?>
I hope all of above script will help you for add date and time in your website.
Thanks
Â
Php Code for getting the current time and date
Â
Php Code for getting the current time and date:
Â
<?php
echo "Date and time (12 hour format): " . date("m/d/y h:m:s ");Â
echo "<br />Date and time (24 hour format): " . date("m/d/y g:m:s");Â
?>
Php code to display time on a webpage
<?php
echo "Current time in 12 hour format: " . date("h:m:s ");Â
echo "<br />Current time in 24 hour format: " . date("g:m:s");Â
?>
Php code to display a date on a webpage
<?php
echo "Current date:" . date("m/d/y");Â
?>