Home arrow PHP arrow Page 2 - Date/Time Processing with PHP

Getting A Date - PHP

Like most programming languages, PHP comes with a fairlyfull-featured API for date and time value manipulation. You've probablyused it in your applications, but never bothered to look too closely atit. Well, here's your chance to rectify that mistake - this articledelves into the date/time API in depth, uncovering some hidden nuggetsand demonstrating how it can be used to simplify date and timeprocessing in your PHP scripts.

TABLE OF CONTENTS:
  1. Date/Time Processing with PHP
  2. Getting A Date
  3. A Stamp In Time...
  4. Race Against Time
  5. When Looks Do Matter
  6. Checking Up
  7. Turning The Tables
By: The Disenchanted Developer, (c) Melonfire
Rating: starstarstarstarstar / 14
March 19, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
First up, before we get into anything too complicated, let's take a quick tour of the important date and time manipulation functions that come with PHP 4.1.
The simplest and most basic thing you'll want to do with PHP's date API is, obviously, get the current date and time. This is best accomplished via the getdate() function, which returns an associative array containing date and time information.

Here's an example:
<?
// get current date information as associative array
$current = getdate();
// print it
print_r($current);
?>
Here's what the output would look like:
Array
(
[seconds] => 7
[minutes] => 19
[hours] => 9
[mday] => 20
[wday] => 3
[mon] => 2
[year] => 2002
[yday] => 50
[weekday] => Wednesday
[month] => February
[0] => 1014176947
)
This information can easily be manipulated and massaged into whatever format you like. For example,

<? // get current date $current = getdate(); // turn it into strings $current_time = $current["hours"] . ":" . $current["minutes"] . ":" . $current["seconds"]; $current_date = $current["mday"] . "." . $current["mon"] . "." . $current["year"]; // print it // this would generate output of the form "It is now 9:14:34 on 23.5.2001" echo "It is now $current_time on $current_date"; ?>
You can also give getdate() a timestamp, and have it convert that timestamp into an array for you. Consider the following example, which sends getdate() a UNIX timestamp for the date December 25 2001, and gets the information back as a much easier-to-read array:

<? print_r(getdate(1009218600)); ?>
The output is:

Array ( [seconds] => 0 [minutes] => 0 [hours] => 0 [mday] => 25 [wday] => 2 [mon] => 12 [year] => 2001 [yday] => 358 [weekday] => Tuesday [month] => December [0] => 1009218600 )
How did I create the timestamp? Flip the page to find out.

 
 
>>> More PHP Articles          >>> More By The Disenchanted Developer, (c) Melonfire
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap

Dev Shed Tutorial Topics: