PHP - Extraction d'une date à partir d'une chaîne de caractère
01/10/2013
Comparaison de différentes méthodes pour extraire le jour, le mois et l'année du chaîne de caractère au format "2011-09-15 00:00:00.000".
4 méthodes testées : substr, explode, preg_match, DateTime.
Résultat des tests :
Le plus rapide est d'utiliser substr, DateTime est beaucoup trop long !
Le code utilisé pour les tests :
4 méthodes testées : substr, explode, preg_match, DateTime.
Résultat des tests :
Test 1 | Test 2 | Test 3 | |
---|---|---|---|
Type | WEB | CLI | WEB |
OS | Debian 6.0.7 | Windows 7 | Linux Mint 15 |
PHP | 5.3.3 | 5.4.12 | 5.4.9 |
Apache | 2.2.16 | - | 2.2.22 |
Substr | 2.7664ms | 0.5547ms | 0.3724ms |
Explode | 4.6522ms | 0.9966ms | 0.6179ms |
PregMatch | 5.7293ms | 1.7454ms | 0.6429ms |
DateTime | 27.9616ms | 7.8209ms | 3.9994ms |
Le plus rapide est d'utiliser substr, DateTime est beaucoup trop long !
Le code utilisé pour les tests :
<?php
if (isset($_SERVER["SERVER_PROTOCOL"]))
echo "<pre>";
$data = array('2011-09-15 00:00:00.000', '2011-09-22 00:00:00.000', '2011-09-23 00:00:00.000', '2011-09-26 00:00:00.000', '2011-09-27 00:00:00.000', '2011-09-28 00:00:00.000', '2011-10-05 00:00:00.000', '2011-10-06 00:00:00.000', '2011-10-07 00:00:00.000', '2011-10-11 00:00:00.000', '2011-10-13 00:00:00.000', '2011-10-19 00:00:00.000', '2011-10-20 00:00:00.000', '2011-10-21 00:00:00.000', '2011-10-25 00:00:00.000', '2011-10-26 00:00:00.000', '2011-10-27 00:00:00.000', '2011-10-31 00:00:00.000', '2011-11-02 00:00:00.000', '2011-11-03 00:00:00.000', '2011-11-04 00:00:00.000', '2011-11-07 00:00:00.000', '2011-11-08 00:00:00.000', '2011-11-09 00:00:00.000', '2011-11-10 00:00:00.000', '2011-11-14 00:00:00.000', '2011-11-15 00:00:00.000', '2011-11-16 00:00:00.000', '2011-11-17 00:00:00.000', '2011-11-18 00:00:00.000', '2011-11-21 00:00:00.000', '2011-11-22 00:00:00.000', '2011-11-23 00:00:00.000', '2011-11-24 00:00:00.000', '2011-11-25 00:00:00.000', '2011-11-28 00:00:00.000', '2011-11-29 00:00:00.000', '2011-11-30 00:00:00.000', '2011-12-01 00:00:00.000', '2011-12-02 00:00:00.000', '2011-12-05 00:00:00.000', '2011-12-06 00:00:00.000', '2011-12-07 00:00:00.000', '2011-12-08 00:00:00.000', '2011-12-09 00:00:00.000', '2011-12-12 00:00:00.000', '2011-12-13 00:00:00.000', '2011-12-14 00:00:00.000', '2011-12-15 00:00:00.000', '2011-12-16 00:00:00.000', '2011-12-19 00:00:00.000', '2011-12-20 00:00:00.000', '2011-12-21 00:00:00.000', '2011-12-22 00:00:00.000', '2011-12-23 00:00:00.000', '2011-12-26 00:00:00.000', '2011-12-27 00:00:00.000', '2011-12-28 00:00:00.000', '2011-12-29 00:00:00.000', '2011-12-30 00:00:00.000', '2012-01-02 00:00:00.000', '2012-01-03 00:00:00.000', '2012-01-04 00:00:00.000', '2012-01-05 00:00:00.000', '2012-01-06 00:00:00.000', '2012-01-09 00:00:00.000', '2012-01-10 00:00:00.000', '2012-01-11 00:00:00.000', '2012-01-12 00:00:00.000', '2012-01-13 00:00:00.000', '2012-01-16 00:00:00.000', '2012-01-17 00:00:00.000', '2012-01-18 00:00:00.000', '2012-01-19 00:00:00.000', '2012-01-20 00:00:00.000', '2012-01-23 00:00:00.000', '2012-01-24 00:00:00.000', '2012-01-25 00:00:00.000', '2012-01-26 00:00:00.000', '2012-01-27 00:00:00.000', '2012-01-30 00:00:00.000', '2012-01-31 00:00:00.000', '2012-02-01 00:00:00.000', '2012-02-02 00:00:00.000', '2012-02-03 00:00:00.000', '2012-02-06 00:00:00.000', '2012-02-07 00:00:00.000', '2012-02-08 00:00:00.000', '2012-02-09 00:00:00.000', '2012-02-10 00:00:00.000', '2012-02-13 00:00:00.000', '2012-02-14 00:00:00.000', '2012-02-15 00:00:00.000', '2012-02-16 00:00:00.000', '2012-02-17 00:00:00.000', '2012-02-20 00:00:00.000', '2012-02-21 00:00:00.000', '2012-02-22 00:00:00.000', '2012-02-23 00:00:00.000', '2012-02-24 00:00:00.000', '2012-02-27 00:00:00.000', '2012-02-28 00:00:00.000', '2012-02-29 00:00:00.000', '2012-03-01 00:00:00.000', '2012-03-02 00:00:00.000', '2012-03-05 00:00:00.000', '2012-03-06 00:00:00.000', '2012-03-07 00:00:00.000', '2012-03-08 00:00:00.000', '2012-03-09 00:00:00.000', '2012-03-12 00:00:00.000', '2012-03-13 00:00:00.000', '2012-03-14 00:00:00.000', '2012-03-15 00:00:00.000', '2012-03-16 00:00:00.000', '2012-03-19 00:00:00.000', '2012-03-20 00:00:00.000', '2012-03-21 00:00:00.000', '2012-03-22 00:00:00.000', '2012-03-23 00:00:00.000', '2012-03-26 00:00:00.000', '2012-03-27 00:00:00.000', '2012-03-28 00:00:00.000', '2012-03-29 00:00:00.000', '2012-03-30 00:00:00.000', '2012-04-02 00:00:00.000', '2012-04-03 00:00:00.000', '2012-04-04 00:00:00.000', '2012-04-05 00:00:00.000', '2012-04-06 00:00:00.000', '2012-04-10 00:00:00.000', '2012-04-11 00:00:00.000', '2012-04-12 00:00:00.000', '2012-04-13 00:00:00.000', '2012-04-16 00:00:00.000', '2012-04-17 00:00:00.000', '2012-04-18 00:00:00.000', '2012-04-19 00:00:00.000', '2012-04-20 00:00:00.000', '2012-04-22 00:00:00.000', '2012-04-23 00:00:00.000', '2012-04-24 00:00:00.000', '2012-04-25 00:00:00.000', '2012-04-26 00:00:00.000', '2012-04-27 00:00:00.000', '2012-04-30 00:00:00.000', '2012-05-02 00:00:00.000', '2012-05-03 00:00:00.000', '2012-05-04 00:00:00.000', '2012-05-07 00:00:00.000', '2012-05-09 00:00:00.000', '2012-05-10 00:00:00.000', '2012-05-11 00:00:00.000', '2012-05-14 00:00:00.000', '2012-05-15 00:00:00.000', '2012-05-16 00:00:00.000', '2012-05-18 00:00:00.000', '2012-05-21 00:00:00.000', '2012-05-22 00:00:00.000', '2012-05-23 00:00:00.000', '2012-05-24 00:00:00.000', '2012-05-25 00:00:00.000', '2012-05-29 00:00:00.000', '2012-05-30 00:00:00.000', '2012-05-31 00:00:00.000', '2012-06-01 00:00:00.000', '2012-06-04 00:00:00.000', '2012-06-05 00:00:00.000', '2012-06-06 00:00:00.000', '2012-06-07 00:00:00.000', '2012-06-08 00:00:00.000', '2012-06-09 00:00:00.000', '2012-06-10 00:00:00.000', '2012-06-11 00:00:00.000', '2012-06-12 00:00:00.000', '2012-06-13 00:00:00.000', '2012-06-14 00:00:00.000', '2012-06-15 00:00:00.000', '2012-06-16 00:00:00.000', '2012-06-17 00:00:00.000', '2012-06-18 00:00:00.000', '2012-06-19 00:00:00.000', '2012-06-20 00:00:00.000', '2012-06-21 00:00:00.000', '2012-06-22 00:00:00.000', '2012-06-23 00:00:00.000', '2012-06-24 00:00:00.000', '2012-06-25 00:00:00.000', '2012-06-26 00:00:00.000', '2012-06-27 00:00:00.000', '2012-06-28 00:00:00.000', '2012-06-29 00:00:00.000', '2012-06-30 00:00:00.000', '2012-07-02 00:00:00.000', '2012-07-03 00:00:00.000', '2012-07-04 00:00:00.000', '2012-07-05 00:00:00.000', '2012-07-06 00:00:00.000', '2012-07-07 00:00:00.000', '2012-07-09 00:00:00.000', '2012-07-10 00:00:00.000', '2012-07-11 00:00:00.000', '2012-07-12 00:00:00.000', '2012-07-13 00:00:00.000', '2012-07-14 00:00:00.000', '2012-07-15 00:00:00.000', '2012-07-16 00:00:00.000', '2012-07-17 00:00:00.000', '2012-07-18 00:00:00.000', '2012-07-19 00:00:00.000', '2012-07-20 00:00:00.000', '2012-07-21 00:00:00.000', '2012-07-22 00:00:00.000', '2012-07-23 00:00:00.000', '2012-07-24 00:00:00.000', '2012-07-25 00:00:00.000', '2012-07-26 00:00:00.000', '2012-07-27 00:00:00.000', '2012-07-28 00:00:00.000', '2012-07-29 00:00:00.000', '2012-07-30 00:00:00.000', '2012-07-31 00:00:00.000', '2012-08-01 00:00:00.000', '2012-08-02 00:00:00.000', '2012-08-03 00:00:00.000', '2012-08-04 00:00:00.000', '2012-08-05 00:00:00.000', '2012-08-06 00:00:00.000', '2012-08-07 00:00:00.000', '2012-08-08 00:00:00.000', '2012-08-09 00:00:00.000', '2012-08-10 00:00:00.000', '2012-08-11 00:00:00.000', '2012-08-13 00:00:00.000', '2012-08-14 00:00:00.000', '2012-08-15 00:00:00.000', '2012-08-16 00:00:00.000', '2012-08-17 00:00:00.000', '2012-08-18 00:00:00.000', '2012-08-20 00:00:00.000', '2012-08-21 00:00:00.000', '2012-08-22 00:00:00.000', '2012-08-23 00:00:00.000', '2012-08-24 00:00:00.000', '2012-08-25 00:00:00.000', '2012-08-27 00:00:00.000', '2012-08-28 00:00:00.000', '2012-08-29 00:00:00.000', '2012-08-30 00:00:00.000', '2012-08-31 00:00:00.000', '2012-09-01 00:00:00.000', '2012-09-02 00:00:00.000', '2012-09-03 00:00:00.000', '2012-09-04 00:00:00.000', '2012-09-05 00:00:00.000', '2012-09-06 00:00:00.000', '2012-09-07 00:00:00.000', '2012-09-08 00:00:00.000', '2012-09-09 00:00:00.000', '2012-09-10 00:00:00.000', '2012-09-11 00:00:00.000', '2012-09-12 00:00:00.000', '2012-09-13 00:00:00.000', '2012-09-14 00:00:00.000', '2012-09-15 00:00:00.000', '2012-09-16 00:00:00.000', '2012-09-17 00:00:00.000', '2012-09-18 00:00:00.000', '2012-09-19 00:00:00.000', '2012-09-20 00:00:00.000', '2012-09-21 00:00:00.000', '2012-09-22 00:00:00.000', '2012-09-23 00:00:00.000', '2012-09-24 00:00:00.000', '2012-09-25 00:00:00.000', '2012-09-26 00:00:00.000', '2012-09-27 00:00:00.000', '2012-09-28 00:00:00.000', '2012-09-30 00:00:00.000', '2012-10-01 00:00:00.000', '2012-10-02 00:00:00.000', '2012-10-03 00:00:00.000', '2012-10-04 00:00:00.000', '2012-10-05 00:00:00.000', '2012-10-06 00:00:00.000', '2012-10-07 00:00:00.000', '2012-10-08 00:00:00.000', '2012-10-09 00:00:00.000', '2012-10-10 00:00:00.000', '2012-10-11 00:00:00.000', '2012-10-12 00:00:00.000', '2012-10-13 00:00:00.000', '2012-10-15 00:00:00.000', '2012-10-16 00:00:00.000', '2012-10-17 00:00:00.000', '2012-10-18 00:00:00.000', '2012-10-19 00:00:00.000', '2012-10-20 00:00:00.000', '2012-10-21 00:00:00.000', '2012-10-22 00:00:00.000', '2012-10-23 00:00:00.000', '2012-10-24 00:00:00.000', '2012-10-25 00:00:00.000', '2012-10-26 00:00:00.000', '2012-10-27 00:00:00.000', '2012-10-28 00:00:00.000', '2012-10-29 00:00:00.000', '2012-10-30 00:00:00.000', '2012-10-31 00:00:00.000', '2012-11-01 00:00:00.000', '2012-11-02 00:00:00.000', '2012-11-03 00:00:00.000', '2012-11-04 00:00:00.000', '2012-11-05 00:00:00.000', '2012-11-06 00:00:00.000', '2012-11-07 00:00:00.000', '2012-11-08 00:00:00.000', '2012-11-09 00:00:00.000', '2012-11-10 00:00:00.000', '2012-11-11 00:00:00.000', '2012-11-12 00:00:00.000', '2012-11-13 00:00:00.000', '2012-11-14 00:00:00.000', '2012-11-15 00:00:00.000', '2012-11-16 00:00:00.000', '2012-11-17 00:00:00.000', '2012-11-19 00:00:00.000', '2012-11-20 00:00:00.000', '2012-11-21 00:00:00.000', '2012-11-22 00:00:00.000', '2012-11-23 00:00:00.000', '2012-11-24 00:00:00.000', '2012-11-25 00:00:00.000', '2012-11-26 00:00:00.000', '2012-11-27 00:00:00.000', '2012-11-28 00:00:00.000', '2012-11-29 00:00:00.000', '2012-11-30 00:00:00.000', '2012-12-01 00:00:00.000', '2012-12-02 00:00:00.000', '2012-12-03 00:00:00.000', '2012-12-04 00:00:00.000', '2012-12-05 00:00:00.000', '2012-12-06 00:00:00.000', '2012-12-07 00:00:00.000', '2012-12-08 00:00:00.000', '2012-12-09 00:00:00.000', '2012-12-10 00:00:00.000', '2012-12-11 00:00:00.000', '2012-12-12 00:00:00.000', '2012-12-13 00:00:00.000', '2012-12-14 00:00:00.000', '2012-12-15 00:00:00.000', '2012-12-17 00:00:00.000', '2012-12-18 00:00:00.000', '2012-12-19 00:00:00.000', '2012-12-20 00:00:00.000', '2012-12-21 00:00:00.000', '2012-12-22 00:00:00.000', '2012-12-23 00:00:00.000', '2012-12-24 00:00:00.000', '2012-12-25 00:00:00.000', '2012-12-26 00:00:00.000', '2012-12-27 00:00:00.000', '2012-12-28 00:00:00.000', '2012-12-29 00:00:00.000', '2012-12-30 00:00:00.000', '2012-12-31 00:00:00.000', '2013-01-01 00:00:00.000', '2013-01-02 00:00:00.000', '2013-01-03 00:00:00.000', '2013-01-04 00:00:00.000', '2013-01-05 00:00:00.000', '2013-01-06 00:00:00.000', '2013-01-07 00:00:00.000', '2013-01-08 00:00:00.000', '2013-01-09 00:00:00.000', '2013-01-10 00:00:00.000', '2013-01-11 00:00:00.000', '2013-01-12 00:00:00.000', '2013-01-13 00:00:00.000', '2013-01-14 00:00:00.000', '2013-01-15 00:00:00.000', '2013-01-16 00:00:00.000', '2013-01-17 00:00:00.000', '2013-01-18 00:00:00.000', '2013-01-19 00:00:00.000', '2013-01-20 00:00:00.000', '2013-01-21 00:00:00.000', '2013-01-22 00:00:00.000', '2013-01-23 00:00:00.000', '2013-01-24 00:00:00.000', '2013-01-25 00:00:00.000', '2013-01-26 00:00:00.000', '2013-01-27 00:00:00.000', '2013-01-28 00:00:00.000', '2013-01-29 00:00:00.000', '2013-01-30 00:00:00.000', '2013-01-31 00:00:00.000', '2013-02-01 00:00:00.000', '2013-02-02 00:00:00.000', '2013-02-03 00:00:00.000', '2013-02-04 00:00:00.000', '2013-02-05 00:00:00.000', '2013-02-06 00:00:00.000', '2013-02-07 00:00:00.000', '2013-02-08 00:00:00.000', '2013-02-09 00:00:00.000', '2013-02-10 00:00:00.000', '2013-02-11 00:00:00.000', '2013-02-12 00:00:00.000', '2013-02-13 00:00:00.000', '2013-02-14 00:00:00.000', '2013-02-15 00:00:00.000', '2013-02-16 00:00:00.000', '2013-02-18 00:00:00.000', '2013-02-19 00:00:00.000', '2013-02-20 00:00:00.000', '2013-02-21 00:00:00.000', '2013-02-22 00:00:00.000', '2013-02-23 00:00:00.000', '2013-02-24 00:00:00.000', '2013-02-25 00:00:00.000', '2013-02-26 00:00:00.000', '2013-02-27 00:00:00.000', '2013-02-28 00:00:00.000', '2013-03-01 00:00:00.000', '2013-03-02 00:00:00.000', '2013-03-03 00:00:00.000', '2013-03-04 00:00:00.000', '2013-03-05 00:00:00.000', '2013-03-06 00:00:00.000', '2013-03-07 00:00:00.000', '2013-03-08 00:00:00.000', '2013-03-09 00:00:00.000', '2013-03-10 00:00:00.000', '2013-03-11 00:00:00.000', '2013-03-12 00:00:00.000', '2013-03-13 00:00:00.000', '2013-03-14 00:00:00.000', '2013-03-15 00:00:00.000', '2013-03-16 00:00:00.000', '2013-03-17 00:00:00.000', '2013-03-18 00:00:00.000', '2013-03-19 00:00:00.000', '2013-03-20 00:00:00.000', '2013-03-21 00:00:00.000', '2013-03-22 00:00:00.000', '2013-03-23 00:00:00.000', '2013-03-24 00:00:00.000', '2013-03-25 00:00:00.000', '2013-03-26 00:00:00.000', '2013-03-27 00:00:00.000', '2013-03-28 00:00:00.000', '2013-03-29 00:00:00.000', '2013-03-30 00:00:00.000', '2013-04-02 00:00:00.000', '2013-04-03 00:00:00.000', '2013-04-04 00:00:00.000', '2013-04-05 00:00:00.000', '2013-04-06 00:00:00.000', '2013-04-07 00:00:00.000', '2013-04-08 00:00:00.000', '2013-04-09 00:00:00.000', '2013-04-10 00:00:00.000', '2013-04-11 00:00:00.000', '2013-04-12 00:00:00.000', '2013-04-13 00:00:00.000', '2013-04-14 00:00:00.000', '2013-04-15 00:00:00.000', '2013-04-16 00:00:00.000', '2013-04-17 00:00:00.000', '2013-04-18 00:00:00.000', '2013-04-19 00:00:00.000', '2013-04-20 00:00:00.000', '2013-04-21 00:00:00.000', '2013-04-22 00:00:00.000', '2013-04-23 00:00:00.000', '2013-04-24 00:00:00.000', '2013-04-25 00:00:00.000', '2013-04-26 00:00:00.000', '2013-04-27 00:00:00.000', '2013-04-28 00:00:00.000', '2013-04-29 00:00:00.000', '2013-04-30 00:00:00.000', '2013-05-01 00:00:00.000', '2013-05-02 00:00:00.000', '2013-05-03 00:00:00.000', '2013-05-04 00:00:00.000', '2013-05-05 00:00:00.000', '2013-05-06 00:00:00.000', '2013-05-07 00:00:00.000', '2013-05-08 00:00:00.000', '2013-05-09 00:00:00.000', '2013-05-10 00:00:00.000', '2013-05-11 00:00:00.000', '2013-05-13 00:00:00.000', '2013-05-14 00:00:00.000', '2013-05-15 00:00:00.000', '2013-05-16 00:00:00.000');
$test = 1000;
$timeExplode = 0;
$timeDateTime = 0;
$timeSubstr = 0;
$timePregMatch = 0;
for ($i = 0; $i < $test; $i++) {
$startTime = microtime(true);
testSubstr();
$timeSubstr += microtime(true) - $startTime;
$startTime = microtime(true);
testExplode();
$timeExplode += microtime(true) - $startTime;
$startTime = microtime(true);
testPregMatch();
$timePregMatch += microtime(true) - $startTime;
$startTime = microtime(true);
testDateTime();
$timeDateTime += microtime(true) - $startTime;
}
echo "Average time for 500 dates\n";
echo "$test tests performed\n";
echo "-------------------\n";
echo "Substr...: " . round(($timeSubstr / $test) * 1000, 4) . "ms\n";
echo "Explode..: " . round(($timeExplode / $test) * 1000, 4) . "ms\n";
echo "PregMatch: " . round(($timePregMatch / $test) * 1000, 4) . "ms\n";
echo "DateTime.: " . round(($timeDateTime / $test) * 1000, 4) . "ms\n";
function testSubstr() {
global $data;
foreach ($data as $dt) {
$year = substr($dt, 0, 4);
$month = substr($dt, 5, 2);
$day = substr($dt, 8, 2);
}
}
function testExplode() {
global $data;
foreach ($data as $dt) {
$tmpDt = explode(" ", $dt);
$tmpDt = explode("-", $tmpDt[0]);
$year = $tmpDt[0];
$month = $tmpDt[1];
$day = $tmpDt[2];
}
}
function testPregMatch() {
global $data;
foreach ($data as $dt) {
preg_match("#([0-9]{4})-([0-9]{2})-([0-9]{2})#", $dt, $matches);
$year = $matches[1];
$month = $matches[2];
$day = $matches[3];
}
}
function testDateTime() {
global $data;
foreach ($data as $dt) {
$date = DateTime::createFromFormat("Y-m-d H:i:s.000", $dt);
$year = $date->format("Y");
$month = $date->format("m");
$day = $date->format("d");
}
}
?>
2 commentaires
Beesofts - 07/01/2014 à 09:20:10
j'ai pris ton fichier pour faire ces tests chez moi :
Test 1 Test 2
OS Windows 7 VM Debian 7.3
PHP 5.5.3 5.5.7
Apache 2.4 2.2
Substr 5.1 0.35
Explode 4.0 0.51
PregMatch 3.2 0.59
DateTime 11.8 2.2
J'obtiens des résultats très similaires en CLI et Web
sykius - 07/01/2014 à 18:07:22
@Beesofts : Merci pour ce retour.
Ton test sous Debian confirme mes tests, par contre celui sous Windows fait l'inverse ...
Flux RSS des commentaires de cet article
Les commentaires sont fermés pour cet article