-3
$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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
WebDawg
  • 1
  • 2
  • Hi, welcome. It would be useful to know what the initial value of `$myDate` is, as this will affect any answers anyone might give you – OllieLowson Mar 10 '23 at 15:00
  • PHP's strtotime expects dates with dashes to be `d-m-Y`, dates with slashes to be `m/d/Y`. Please see the note here: https://www.php.net/manual/en/function.date-create.php#123697 -- This used to be noted in the documentation, but I can no longer find it. – aynber Mar 10 '23 at 15:06
  • https://stackoverflow.com/a/11435513/11061164 The answer by ceiroa is the best way to handle different PHP date formats. – mark_b Mar 10 '23 at 16:07
  • 1
    Please come up with a better title. This title is very bad and does not describe the topic well – Dharman Mar 11 '23 at 00:51

3 Answers3

1

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); ?>
0

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

JDarwind
  • 97
  • 6
-2

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.

  • PHP is a server-side language, and as such it is not influenced by the user's PC time locale. Please provide valid examples or documentation when answering questions and abstain from using guesses. – Victor Marcoianu Mar 16 '23 at 11:15