Articles
Ce document est aussi disponible en français
Who never fought with explode(), list(), and other functions to get the parts of a date?
This time is over with date_parse()
date_parse() returns an array with all the details on the date.
Example 1 - Getting details on a date
<?php var_export(date_parse("03/01/2007 12:14")); /* output : array ( 'year' => 2007, 'month' => 3, 'day' => 1, 'hour' => 12, 'minute' => 14, 'second' => 0, 'fraction' => 0, 'warning_count' => 0, 'warnings' => array ( ), 'error_count' => 0, 'errors' => array ( ), 'is_localtime' => false, )*/ ?>Exemple 2 - date_parse() tries hard to understand
<?php var_export(date_parse("03/01/2007 12:14")); var_export(date_parse("2007/03/01 12:14")); var_export(date_parse("12:14 2007/03/01")); var_export(date_parse("12:14:00.001 2007/03/01")); var_export(date_parse("12.14.00.001 2007-03-01")); ?>Exemple 3 - date_parse() gives away error messages in a dedicated index
<?php print_r(date_parse("10:99:00.5 12-12-106")); /* Array ( [year] => 106 [month] => 12 [day] => 12 [hour] => 10 [minute] => 9 [second] => 0 [fraction] => 0 [warning_count] => 0 [warnings] => Array ( ) [error_count] => 1 [errors] => Array ( [4] => Double time specification ) [is_localtime] => ) */ ?>
Keep in mind
- date_parse() is available since PHP 5.2 : upgrade!
- date_parse() accepts year values from 100 to 9999 : below 100, we are back to year 2000.
- date_parse() is able to parse directly MySQL dates
- date_parse() may also handle time zones
| < Précédent | Suivant > |
|---|
Commentaires
Vous pouvez ajouter votre commentaire! |
Vous devez vous connecter pour commenter


