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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:51:36+00:00 2026-06-18T04:51:36+00:00

Can I compare two dates in a string like numbers? Or should I retype

  • 0

Can I compare two dates in a string like numbers? Or should I retype them to ints?

Is this code ok?

<?php
$date1 = "20130102";
$date2 = "20151012";

if ($date1 < $date2){
    echo "date1 < date2";
} elseif ($date1 > $date2){
    echo "date1 > date2";
} else {
    echo "date1 = date2";
}
?>
  • 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-18T04:51:37+00:00Added an answer on June 18, 2026 at 4:51 am

    Your code is safe and correct under the following condition: Iff your dates are formatted like Ymd (according to php’s date function)

    The reason this works is that in all character sets that I can imagine the characters 0-9 sort exactly like the numbers 0-9 do. If you then use leading 0s your strings sort just like numbers.

    N.B. that using strings as a data type for dates is a code smell. See this page (no.7)

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

Sidebar

Related Questions

How can compare two dates like this format var CurrDate = new Date().format(MM/dd/yyyy); if
How can I compare two dates in PHP? The date is stored in the
im trying to compare two dates like this ICriteria criteria = base.DataStore.TransactionScope.NHibernateSession.CreateCriteria<CcCorte>(); criteria.Add(Restrictions.Ge(Start, init.Date));
I am looking for a gem that can compare two strings (in this case
is there a way l can compare two columns | fields in django like
I would like to know if there is anyway I can compare two columns
I have two fields: string date1 = 04/26/10; string date2 = 04/25/10; How can
I have two dates, formated like "Y-m-d H:i:s". I need to compare these two
I want to compare two string ignoring real time factors like date and time
Possible Duplicate: How can I compare two dates, return a number of days I

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.