2.16 Loading Today s Date into an Array


2.16 Loading Today's Date into an Array

You want to load today's date and time into an array for further manipulation.

Technique

Use PHP's getdate() function, which returns an associative array of seconds, minutes, hours, mday (day of the month), wday (day of the week), mon (current month number), year (number), yday (day of the year as a number), weekday (day of the week in text form), and month (month of the year in text form):

 <?php $date_ar = getdate(); ?> 

Comments

This is very useful in many ways. It enables us to do things such as format the current date in MM/DD/YYYY format:

 <?php $da = getdate(); $datestamp = "$da[mday]/$da[mon]/$da[year]"; ?> 

In this case, you can also use the date() function, which returns a formatted string according to the arguments that you provide:

 <?php $datestamp = date ("j/n/Y"); ?> 

If you are looking for C/Perl compatibility, you can use PHP's localtime() function, which returns a numerically indexed array with the current time or an associative array with the current time. The indexes have the same names as the corresponding C structure elements.

 <?php $date1 = localtime(); $date2 = localtime (time(), 1); print "$date1[5] is the same as $date2[tm_year]"; ?> 


PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net