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

  • SEARCH
  • Home
  • 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 9270705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:26:10+00:00 2026-06-18T15:26:10+00:00

I have run across php code the compares dates in YYYY-mm-dd format as strings.

  • 0

I have run across php code the compares dates in YYYY-mm-dd format as strings. Does this work? It seems to work in simple cases, but I am not sure it makes sense to compare them as strings.

<?php
$today = '2013-02-11';
$start_date = '2013-02-11';
$end_date = '2013-02-12';
$on_promo = (($start_date <= $today) && ($end_date >= $today));

if ($on_promo)
{
    echo 'ON promo';
}
else
{
    echo 'OFF promo';
}
?>
  • 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-06-18T15:26:11+00:00Added an answer on June 18, 2026 at 3:26 pm

    When comparing strings in PHP using greater than or less than, it compares them in alphabetical order.

    Alphabetically 2013-02-10 comes before 2013-02-13

    If we have:

    $date1 = '2013-02-10';
    $date2 = '2013-02-13';
    var_dump($date2 > $date1); // produces true
    
    var_dump('apple' > 'banana'); // produces false
    

    However, note that if the strings are both numerical, it will cast them both to integers

    var_dump('11' > '101'); // produces false
    var_dump('a11' > 'a101'); // produces true
    var_dump('11a' > '101a'); // produces true
    

    Therefore if using the format YYYY-MM-DD you can compare two dates perfectly fine, however I really don’t recommend relying on this. Someone might throw in a date like 2013-2-11 (note the month doesn’t have the leading 0) and it will completely throw off the logic. It is much better to take John Conde’s suggestion and use DateTime

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

Sidebar

Related Questions

I have run this Perl code: #!/usr/bin/perl print content-type: text/html \n\n; print Hello World.\n;
I'm doing some refactoring work on PHP/MySQL code and stumbled across an interesting problem,
I'm working on an EntityFramework application and I have run across a problem when
While building several different projects in QtCreator, I have run across the following build
In all my years of .NET programming I have not run across a bug
I have run this command with root: [root@localhost git-shell-commands]# ssh git@192.168.1.12 git@192.168.1.12's password: Last
Working in some legacy code, I've run across a ton of Try/Catch statements. Try/Catch
I've run across an interesting PHP/SOAP error that has me stymied. After searching I
I have this code below which is called by running the getGrades function. function
I have followed a tutorial to create a simple blog writing application in PHP

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.