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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:37:35+00:00 2026-06-12T08:37:35+00:00

I am having trouble with the strtotime() in my code to separate date. When

  • 0

I am having trouble with the strtotime() in my code to separate date. When i use “/” the format becomes mm-dd-yyyy and when i use”-” the format becomes dd-mm-yyyy. I am trying to use both “/” and “-” when the user inputs the date
I have used preg_macth to validate weather the user has given – or /
A big thanks to the guy who can help me with this.
My code is given below

<?php
$date1=$_GET["q1"];
$date2= $_GET["q2"];
// To check date format for From date   
function checkDateFormat1($date1)
{
//match the format of the date
    if( (preg_match(" /^(\d{1,2})\-(\d{1,2})\-(\d{4})$/", $date1,$parts)) || (preg_match(" /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/", $date1,$parts)) )
{
    //check weather the date is valid of not
    //if(checkdate($parts[2],$parts[3],$parts[1]))
    if(checkdate($parts[2],$parts[1],$parts[3]))
    return true;
    else
    return false;
}
else
return false;
}

 // To check the format for To date         
 function checkDateFormat2($date2)
{

if((preg_match(" /^(\d{1,2})\-(\d{1,2})\-(\d{4})$/", $date2,$parts))||(preg_match(" /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/", $date2,$parts)))
{
    //check weather the date is valid of not
    //if(checkdate($parts[2],$parts[3],$parts[1]))
    if(checkdate($parts[2],$parts[1],$parts[3]))
    return true;
    else
    return false;
}
else
return false;
 }                      

if ((checkDateFormat2($date2) && checkDateFormat1($date1))==1)
{
if ($date1==$date2)
{
    $days="1";
    echo $days;
    exit;
}
else
{   
    $difference =(strtotime($date2) - strtotime($date1));
    //calculate the number of days
    $days = round(((($difference/60)/60)/24), 0);
}
 }
 else
 {
     echo "Invalid Date Format"."<br>";
echo "Please make sure that your for date format is MM-DD-YYYY";

exit;
        }

  if ($days<0)
 {
echo "From date cannot be greater than to date";

}   
else
{
$days++;
echo $days;


}   
 ?>
  • 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-12T08:37:36+00:00Added an answer on June 12, 2026 at 8:37 am

    It is working exactly like strToTime() is supposed to work. From the php manual:

    Note:

    Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

    To avoid potential ambiguity, it’s best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.

    http://php.net/manual/en/function.strtotime.php

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

Sidebar

Related Questions

Having trouble with this code, wondering if I'm trying to achieve something impossible. I'm
I'm having trouble with this query. I have 2 date coming from textboxes (don't
Having trouble with each function... Will try to explain by example... In my code,
Having trouble accessing javascript code in a mixed html/js ajax response. jQuery ajax doc
Having trouble framing this assertion in Rspec for my rails app. A User has
Having trouble with the following segment of code. I'm getting a parameter count mismatch.
Having Trouble with Entity Framework. I have been populating EntityReferences with an EntityKey inorder
Having trouble with some select statements. I have 2 tables. sms_log and sms_messages. sms_log
Having trouble to start a new activity after progressbar have loaded. Im getting stucked
Having trouble trying to turn an XDocument into an list of objects, in particular

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.