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 6931141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:32:19+00:00 2026-05-27T11:32:19+00:00

So, PHP apparently feels like being a moron today. Or maybe it’s me. Or

  • 0

So, PHP apparently feels like being a moron today. Or maybe it’s me. Or both.

function before($test,$bar) {
    $test   = date_create($test);
    $bar    = date_create($bar);

    $diff   = date_diff($bar,$test);

    $diff   = $diff->format('%r%a') * 1;

    return $diff<0;
}

It refuses to accept that a date generated by the following means:

date('m-d-Y', strtotime($date));

…is anything but a BOOLEAN! If I output the result, it comes out as a string, but – with PHP pining to confound me – before() treats it like something that it isn’t. There is NOTHING here that should convert it to a boolean. I can feed it a string directly, and it works fine. Give it a date from a piece of code specifically made for the very purpose, well, we can’t have that ’cause…

Warning: date_diff() expects parameter ... to be DateTime, boolean given

I thought, “fine, I’ll give you your parser’s desire.”

function before($test,$bar) {
    $test   = new DateTime(date('Y-m-d',strtotime($test)));
    $bar    = new DateTime(date('Y-m-d',strtotime($bar)));

    $diff   = $bar->diff($test);

    $diff   = $diff->format('%r%a') * 1;

    return $diff<0;
}

No change in result whatsoever. I gave it what it wanted, or at least what it said it wanted (I think), and it still rejects me. Coding mimicking reality.

I’m not terribly comfortable with dates yet. I’m also not terribly comfortable yet with how PHP handles the time of year. Any advice? And maybe how to get this to work, too?

Edit

Running var_dump($test)

object(DateTime)#7 (3) {
  ["date"]=>
      string(19) "1970-01-01 00:00:00"
  ["timezone_type"]=>
      int(3)
  ["timezone"]=>
      string(13) "Europe/Berlin"
}
  • 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-27T11:32:20+00:00Added an answer on May 27, 2026 at 11:32 am

    As explained in the manual, date_create() is an alias for DateTime::__construct(). And there we can read:

    Return Values
    Returns a new DateTime instance. Procedural style returns FALSE on failure.

    So it’s pretty clear where the boolean comes from.

    Now, your code shows many conversions from Unix timestamp to string and viceversa but not the value you start from, so it’s impossible to point out the exact source of the problem. But I’d say you have certain confusion about the data types involved in date handling. Here’s a little summary:

    • Unix timestamp: it’s an integer that counts the number of seconds since the Unix Epoch (Jan 1970). It’s the value handled by legacy date functions that receive an integer as argument or return one.

    • DateTime: it’s the new object-oriented date feature introduced in PHP/5.2.

    • Strings: it’s not a standardised format per-se so it should only be used for display purposes or as intermediate format (for instance, to insert a date into a database).

    Sadly, the DateTime constructor only accepts strings and does not even allow to tell the exact format (IMHO, a not well-thought design decision). To reduce the potential ambiguity (is 10/11/2011 10th Nov. or 11 Oct?), I suggest you use the "YYYY-MM-DD" format.

    A tip: decide whether to use timestamps or DateTime objects and stick to that. It’ll make your live easier.

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

Sidebar

Related Questions

I have like 50 different php files which all use the mail function. After
I'm using the Yii PHP framework which has a function PDO::lastInsertId that's apparently just
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
PHP has a very nice function, isset($variableName). It checks if $variableName is already defined
PHP (among others) will execute the deepest function first, working its way out. For
I'm working on a PHP script on a remote server-- apparently the caching is
My background is php so entering the world of low-level stuff like char is
Why is it that installing mod_mono may uninstall the PHP Apache module? Apparently they
I have got a problem where my class apparently is not being parsed correctly

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.