Articles

The month of PHP functions : analyzing a date

  • Ecrit par Julien Pauli
  • mercredi 11 avril 2007
Image pour le titre du contenu

Ce document est aussi disponible en français fr 

   

  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

< Précédent   Suivant >

Commentaires

Vous pouvez ajouter votre commentaire!


Vous devez vous connecter pour commenter