Asked  7 Months ago    Answers:  5   Viewed   41 times

I'm looking for a function identical to DateTime::createFromFormat but I need it to work in an environment running a version of PHP which is older than v5.3. Basically I need to provide a format, like you'd use with the Date() function, and I then need to parse/validate a string against that format, and return a timestamp if the string is formatted correctly and a valid date.

Anyone know where I can find something like that, or do I have to write it myself?

Again, this has to work with a specific format, provided as an argument. The format could be anything, so there's no guarantee I can just use strtotime().



DateTime::createFromFormat and date_parse_from_format have been added in PHP 5.3 because there was a high demand for that feature, especially from developpers who code for users who don't use US date/time formats.

Before those, you had to develop a specific function to parse the format you were using ; with PHP < 5.3, what is generally done is :

  • Decide which format will be accepted by the application
  • Display some message saying something like "your input should be JJ/MM/AAAA" (French for DD/MM/YYYY)
  • Check that the input is OK, regarding to that format
  • And parse it to convert it to a date/time that PHP can understand.

Which means applications and developpers generally didn't allow for that many formats, as each format meant one different additionnal validation+parsing function.

If you really need that kind of function, that allows for any possible format, I'm afraid you'll kind of have to write it yourself :-(

Maybe taking a look at the sources of date_parse_from_format could help, if you understand C code ? It should be in something like ext/date/php_date.c -- but doesn't seem to be that simple : it's calling the timelib_parse_from_format function, which is defined in ext/data/lib/parse_date.c, and doesn't look that friendly ^^

Friday, May 28, 2021
answered 7 Months ago

I have found out the solution:

Method _formatAsDate in the file PHPExcel/Style/NumberFormat.php

The method _formatAsDate in NumberFormat.php

if the date is like 16/11/2014, when passed to strtotime will result in false as the date is supposed to be in format m/d/Y by strtotime. So if you change the format to m/d/Y if it's d/m/Y then the solution will always be correct.


  1. 16/11/2014==1970-01-01 (Row: 1)
  2. 16/11/2014==1970-01-01 (Row: 2)
  3. 23/12/2014==1970-01-01 (Row: 3).


  1. 11/16/2014==2014-11-16 (Row: 1)
  2. 11/16/2014==2014-11-16 (Row: 2)
  3. 12/23/2014==2014-12-23 (Row: 3)

Code is still the same and simple to import the file:

protected function importExcel($filePath) {
    $excelData = array();
    if ($filePath) {
        $objPHPExcel = PHPExcel_IOFactory::load($filePath);
        foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
            $worksheetTitle = $worksheet->getTitle();
            $highestRow = $worksheet->getHighestRow(); // e.g. 10
            $highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
            $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
            $nrColumns = ord($highestColumn) - 64;
            $data = array();
            for ($row = 1; $row <= $highestRow; ++$row) {
                $values = array();
                for ($col = 0; $col < $highestColumnIndex; ++$col) {
                    $cell = $worksheet->getCellByColumnAndRow($col, $row);
                    $val = $cell->getValue();
                    if (isset($val) && $val)
                        $data[$row][$col] = $val;
            $excelData[$worksheetTitle] = $data;
        return $excelData;
    return FALSE;
Wednesday, March 31, 2021
answered 9 Months ago

I ran into the same issue and found the following solution in the documentation:

To run your functional tests, the WebTestCase class bootstraps the kernel of your application. In most cases, this happens automatically. However, if your kernel is in a non-standard directory, you'll need to modify your phpunit.xml.dist file to set the KERNEL_DIR environment variable to the directory of your kernel:

    <!-- ... -->
        <server name="KERNEL_DIR" value="/path/to/your/app/" />
    <!-- ... -->

So check your phpunit.xml.dist configuration file and try to add the absolute path to your app-directory.

Hope it helps.

Friday, July 30, 2021
Bálint Molnár
answered 5 Months ago

Ok, i answered my own question.

The key is to track how many rounds of division the for loop goes through until the quotient of the current time minus the input time, $difference, divided by $jth value of the $lengths array item is less than the $j+1th value of this array.

I track this by incrementing the variable $i (notice the if/elseif/else clause demonstrating each of the 3 points I mention above) in this modified version of nicetime():

  function nicetime($date)
    if(empty($date)) {
      return "No date provided";

$periods         = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths         = array("60","60","24","7","4.35","12","10");

$now             = time();
$unix_date         = strtotime($date);

   // check validity of date
if(empty($unix_date)) {    
    return "Bad date";

// is it future date or past date
if($now > $unix_date) {    
    $difference     = $now - $unix_date;
    $tense         = "ago";

} else {
    $difference     = $unix_date - $now;
    $tense         = "from now";
for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
    $difference /= $lengths[$j];

$difference = round($difference);

if($difference != 1) {
    $periods[$j].= "s";

    $day="$difference $periods[$j] {$tense}";
    return $day;
    //satisfies case #1 where time is listed as seconds, minutes, hours ago

    $difference == 1 && $periods[$j]=='day' ? $day='yesterday':     
    $day="$difference $periods[$j] {$tense}";
    return $day;        
    //satisfies case #2 where time is listed as yesterday if not the current day

    return $date;       
    // satisfies case #3 where date is listed as M/D/Y if greater than a week old


echo "case#1: ".nicetime('2012-08-13 23:12:16');
echo "case#2: ".nicetime('2012-08-12 23:12:16');
echo "case#3: ".nicetime('2012-07-07 23:12:16');
Saturday, August 21, 2021
answered 4 Months ago

Silly mistake on my part... simply forgot to add phpunit as a dependency in the project. For anyone else that gets this error, to composer.json add:

"require-dev": {
    "phpunit/phpunit": "3.7.*"

And then run:

composer update

That solved the problem.

Friday, September 17, 2021
Samir Sabri
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :