Sunday, November 13, 2011

How to display date in PHP

To display date in PHP we use date() function.


Example-1:


<?php echo date("d-m-Y"); ?>


Output: 13-11-2011


To display time in PHP we can use the same function like this


Example-2
<?php echo date("h:i:s");  ?> //Outputs hour:minute:seconds as below


Output: 06:09:08


Oops don't panic if the time displayed on your screen is different from your clock. To correct it you have to set the default time-zone as below:



<?php 
   date_default_timezone_set('Asia/Kolkata'); //mine is Asia/Kolkata
   echo date("h:i:s"); 
?>


You may have noticed that in example1 date("d-m-Y") d is small, m is small, Y is capitalized. Is there any relevance in doing so? Yes of-course, there is.


echo date("d-m-y"); //outputs 13-11-11
echo date("d-M-y"); //outputs 13-Nov-11
echo date("D-M-y"); //outputs Sun-Nov-11


To get the complete chart CLICK HERE




No comments:

Post a Comment