سؤال

I have $date_string = '12/1/2014';

I need:

  1. To parse it into a timestamp number (to save in a DB)
  2. And then parse that timestamp to output in a different format, e.g. December 1, 2014

I am assuming that using the DateTime class would be the preferred method (and I personally find it very convenient).

// timestamp
$date_string = '12/1/2014'; // input
$d1 = new DateTime($date_string);
$date_timestamp = $d1->getTimestamp(); // output

// re-format
$date_timestamp = '1417410000'; // input
$d2 = new DateTime("@$date_timestamp"); // append @ to hint the timestamp
$date_formatted = $d2->format('F j, Y'); // output

However, I am curious what would be the fastest way to perform both parse operations.

هل كانت مفيدة؟

المحلول

In a 100,000 loop:

  • date() = 3.221485 sec
  • date_parse() = 1.090016 sec
  • date_parse_from_format() = 0.871224 sec (the fastest!)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top