Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4599976
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:37:05+00:00 2026-05-21T23:37:05+00:00

PHP date_create_from_format function is accepting non-existent, though format-valid dates. I would like this function

  • 0

PHP date_create_from_format function is accepting non-existent, though format-valid dates.

I would like this function to behave like the date command:

niloct@HP-Mini:~$ date --date="29/02/2011" +%s
date: invalid date `29/02/2011'

though this is what happens in php:

$tmp = date_create_from_format('d/m/Y H:i:s',"29/02/2011 00:00:00", timezone_open('America/Sao_Paulo'));
var_dump($tmp);
/*
output: object(DateTime)#28 (3) { ["date"]=> string(19) "2011-03-01 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(17) "America/Sao_Paulo" }
*/

Can this automatic conversion be avoided and the function return -1 in this case ?

niloct@HP-Mini:~$ php -v
PHP 5.3.3-1ubuntu9.3 with Suhosin-Patch (cli) (built: Jan 12 2011 16:08:14) 

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T23:37:06+00:00Added an answer on May 21, 2026 at 11:37 pm

    I found that calling date_get_last_errors after date_create_from_format already gives you all kind of validations on the parsed string, in the arrays warnings and errors, and counters warning_count and error_count.

    So just checking the counters for zero warnings and errors is enough.

    Revised code with some tests:

    <?
    $appTZ = 'America/Sao_Paulo';
    
    function datetimeCreateErrorsWarnings() {
        $obj = date_get_last_errors();  
        return ($obj['error_count'] > 0) || ($obj['warning_count'] > 0);
    }
    
    function datetimeToUnixTS($data) {
        global $appTZ;
    
        $fmt = "d/m/Y H:i:s";
    
        $dtobj = date_create_from_format($fmt, $data . " 00:00:00", timezone_open($appTZ)); 
    
        if ($dtobj) {
            if (!datetimeCreateErrorsWarnings()) {
                return date_timestamp_get($dtobj);
            }
        }
    
        //Second try, $data is auto-suficient
        $dtobj = date_create_from_format($fmt, $data, timezone_open($appTZ));
    
        if ($dtobj) {
            if (!datetimeCreateErrorsWarnings()) {
                return date_timestamp_get($dtobj);
            }
        }
    
        return -1;
    }
    
    $test = array(
        'asdfasdf',
        '',
        "28/02/2011",
        "29/02/2011",
        "28/02/2011 00:00:90"
    );
    
    for ($i=0; $i<count($test); $i++) {
        $tmp = datetimeToUnixTS($test[$i]);
        var_dump($tmp);
        echo "<p>";
    }
    ?>
    

    The above script calculates the unix timestamp of the datetime created.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best (date format independent way) in PHP to calculate difference in
I have: <?php $file=fopen(date(Y-m-d)..txt,r+) or exit(Unable to open file!); if ($_POST[lastname] <> ) {
For PHP I have a date I want line wrapped. I have $date =
I am planning a PHP application that needs to store date/times in an MSSQL
How to calculate minute difference between two date-times in PHP?
PHP has a great function called htmlspecialcharacters() where you pass it a string and
PHP's explode function returns an array of strings split on some provided substring. It
I have asked aqbout timezones and date/time before but this is a more specific
I'm using this piece of php code to create and rotate an image. And
I am currently working on this project which requires me to make a function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.