KEMBAR78
A simple php exercise on date( ) function | PDF
A	
  simple	
  PHP	
  exercise	
  on	
  date(	
  )	
  
	
  
Exercise:	
  
Given	
  the	
  following	
  codes:	
  
	
  

Today's date is
<?php
echo date("");
?>
<br>
The time now on the server is
<?php
echo date("");
?>

	
  
Modify	
  the	
  code	
  so	
  that	
  you	
  will	
  get	
  the	
  following	
  output:	
  
	
  
Today's date is Thu, 16-Jan-2014
The time now on the server is 13:12:04

	
  
	
  
	
  
Part	
  1	
  Step	
  by	
  Step:	
  

	
  
Step	
  1:	
  
	
  
From	
  the	
  given	
  information,	
  we	
  are	
  able	
  to	
  put	
  in	
  the	
  comma,	
  the	
  blank	
  space	
  
and	
  the	
  dashes	
  into	
  the	
  date(	
  )	
  function	
  as	
  follows:	
  
	
  

Today's	
  date	
  is	
  	
  
<?php	
  
	
  	
  echo	
  date("?,	
  	
  ?-­‐?-­‐?");	
  
?>	
  
	
  

Step	
  2:	
  find	
  the	
  appropriate	
  format	
  to	
  replace	
  the	
  ?	
  
	
  
Locate	
  the	
  php	
  Manual	
  by:	
  
	
  
1. Google	
  “php	
  manual”	
  and	
  click	
  on	
  the	
  appropriate	
  link:	
  
	
  

	
  

	
  
 
2. Use	
  the	
  searchbox	
  on	
  the	
  right	
  side,	
  type	
  “date”	
  and	
  click	
  on	
  the	
  “date”	
  
function:	
  
	
  

	
  
3. Refer	
  to	
  the	
  formats	
  listed,	
  such	
  as:	
  
	
  

	
  

	
  

:	
  

	
  

:	
  

	
  

:	
  
	
  
4. Put	
  the	
  above	
  format	
  into	
  the	
  date(	
  )	
  function	
  and	
  run	
  the	
  code	
  through	
  your	
  
XAMPP	
  or	
  WAMP	
  servers:	
  

	
  
Today's	
  date	
  is	
  	
  
<?php	
  
	
  	
  echo	
  date("D,	
  	
  d-­‐M-­‐Y");	
  
?>	
  
	
  
Output:	
  
	
  

Today's date is Tue, 21-Jan-2014

	
  
	
  
Part	
  2:	
  
Now	
  try	
  Part	
  2	
  on	
  your	
  own.	
  	
  Answer	
  on	
  the	
  next	
  page.	
  
	
  

The	
  time	
  now	
  on	
  the	
  server	
  is	
  	
  
<?php	
  
	
  	
  echo	
  date("");	
  
?>	
  
	
  

The time now on the server is 13:12:04

	
  
	
  

	
  

	
  
Solution:	
  
	
  

The	
  time	
  now	
  on	
  the	
  server	
  is	
  	
  
<?php	
  
	
  	
  echo	
  date("G:i:s");	
  
?>	
  
	
  
	
  
Extra	
  –	
  setting	
  the	
  correct	
  timezone	
  
There	
  are	
  two	
  ways	
  to	
  set	
  the	
  timezone:	
  
	
  
1. Use	
  the	
  date.timezone	
  setting	
  in	
  the	
  php.ini	
  file	
  –	
  this	
  will	
  affect	
  all	
  php	
  
files.	
  
	
  

	
  
Change	
  from	
  the	
  default	
  to	
  your	
  own	
  timezone	
  eg	
  Asia/Singapore	
  
	
  

	
  

	
  
	
  
2. Use	
  the	
  date_default_timezone_set()	
  function	
  before	
  the	
  date(	
  )	
  function–	
  
this	
  will	
  only	
  affect	
  the	
  current	
  file.	
  
	
  

Today's	
  date	
  is	
  	
  
<?php	
  
	
  	
  date_default_timezone_set("Asia/Singapore");	
  
	
  	
  echo	
  date("D,	
  	
  d-­‐M-­‐Y");	
  
?>	
  

A simple php exercise on date( ) function

  • 1.
    A  simple  PHP  exercise  on  date(  )     Exercise:   Given  the  following  codes:     Today's date is <?php echo date(""); ?> <br> The time now on the server is <?php echo date(""); ?>   Modify  the  code  so  that  you  will  get  the  following  output:     Today's date is Thu, 16-Jan-2014 The time now on the server is 13:12:04       Part  1  Step  by  Step:     Step  1:     From  the  given  information,  we  are  able  to  put  in  the  comma,  the  blank  space   and  the  dashes  into  the  date(  )  function  as  follows:     Today's  date  is     <?php      echo  date("?,    ?-­‐?-­‐?");   ?>     Step  2:  find  the  appropriate  format  to  replace  the  ?     Locate  the  php  Manual  by:     1. Google  “php  manual”  and  click  on  the  appropriate  link:        
  • 2.
      2. Use  the  searchbox  on  the  right  side,  type  “date”  and  click  on  the  “date”   function:       3. Refer  to  the  formats  listed,  such  as:         :     :     :     4. Put  the  above  format  into  the  date(  )  function  and  run  the  code  through  your   XAMPP  or  WAMP  servers:     Today's  date  is     <?php      echo  date("D,    d-­‐M-­‐Y");   ?>     Output:     Today's date is Tue, 21-Jan-2014     Part  2:   Now  try  Part  2  on  your  own.    Answer  on  the  next  page.     The  time  now  on  the  server  is     <?php      echo  date("");   ?>     The time now on the server is 13:12:04        
  • 3.
    Solution:     The  time  now  on  the  server  is     <?php      echo  date("G:i:s");   ?>       Extra  –  setting  the  correct  timezone   There  are  two  ways  to  set  the  timezone:     1. Use  the  date.timezone  setting  in  the  php.ini  file  –  this  will  affect  all  php   files.       Change  from  the  default  to  your  own  timezone  eg  Asia/Singapore           2. Use  the  date_default_timezone_set()  function  before  the  date(  )  function–   this  will  only  affect  the  current  file.     Today's  date  is     <?php      date_default_timezone_set("Asia/Singapore");      echo  date("D,    d-­‐M-­‐Y");   ?>