skip to Main Content
$myDate = date('m-d-Y 00:00:00', strtotime($myDate));
if($debugs)echo"<br>myDate is: $myDate in line: ".__line__."<br>";
$mysqlstart = date('Y-m-d 00:00:00', strtotime($myDate));
if($debugs)echo"<br>mysqlstart is: $mysqlstart in line: ".__line__."<br>";

From the code above, I would like to show $myDate to the audience while passing $mysqlstart into a query.

When I echo my query, the month is in the date spot and the date is in the month spot
so
instead of getting $mysqlstart as ‘2023-03-10’, I get an output of ‘2023-10-03’. Very odd.

I tried the above code expecting the date and month to switch for sql purposes but it exchanges the month and date in the incorrect pattern.

3

Answers


  1. This is only a guess but is your pc in setup in English or United Kingdom or United States I think this is your problem good luck let me know if you get it fixed thanks Ian.

    Login or Signup to reply.
  2. What’s the previous value of $myDate? In the first line of your code you are using it to pass its value to strtotime. Try something like this to get dates, obviously you can pass to mktime the date you want:

    <?php $myDate = mktime(0,0,0,03,25,2023);
    echo $mysqlstart = date('Y-m-d H:i:s', $myDate); ?>
    
    Login or Signup to reply.
  3. You can use the DateTime class to achive the result.

    here a small example

    <?php
    // Creating a new Datetime Object
    $myDate = new DateTime();
    
    //Setting the time at 00:00:00
    $myDate->setTime(00,00,00);
    
    //Formatting the date in a readable format
    $myDateReadable = $myDate->format('m-d-Y H:i:s');
    
    //Print out the readable version of the date for debug
    echo $myDateReadable;
    
    //adding a new line to get a clearer output.
    //if you need to use this in the browser, you need to use the br tag instead of n or the nl2br function.
    echo "n";
    //Formatting the date in a sql convenient way format
    $mysqlstart = $myDate->format('Y-m-d H:i:s');
    
    //Print out the generate date
    echo $mysqlstart;
    

    The output of the script will be the following:

    03-10-2023 00:00:00
    2023-03-10 00:00:00
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search