Hi I set the time zone
date_default_timezone_set('Europe/London');
but when I call
echo date("d-M-Y h:m:s", time()). "<br>";

I get
23-Mar-2023 01:03:36

It thinks mit is one oclock in the morning.

    Ah. I think it miust be set to AM/PM. can I change this? Up to know it has always been 24H without setting local time.

      Okay. I used to use validateDate() but that must no longer be used. I have to use checkdate and it has to be american format month day year. Is their an easyer way to validate a date than this?

      	$x = hexdec($testdateX);
      	$date = date("dmY", $x);
      	$split = str_split($date);
      	$d =  $split[0] . $split[1];
      	$m =  $split[2] . $split[3];
      	$y =  $split[4] . $split[5] . $split[6] . $split[7];
      	$valid = checkdate($m, $d, $y);
      	echo $valid;
      

        What is $testdateX and why do you need to convert it from hexadecimal to decimal? (Not saying it's wrong, just want to understand what is going on.) Since you are treating it as a UNIX timestamp (which is simply the number of seconds since the arbitrary start time of the "UNIX epoch") integer in the following date() call, then almost by definition its output is "correct" (as long as the argument is, in fact, an integer). Trying to validate it by converting it into a string, and then parsing that string into into separate parts, and then checking those parts in checkdate() seems completely redundant to me: you're essentially testing whether the PHP date/time functions you used "work" or not.

          Consider reading the documentation on checkdate and you'll see that it assumes the supplied params are integers. Check the documentation on date and if you read the comments, you'll realize that unix timestamps don't have a timezone associated with them, and there's some conversion that'll happen to whatever is set as the default timezone on your machine.

          You might also clean up your code a bit by using a space as a deliminter when you convert $testdateX to a date string.

          	$x = hexdec($testdateX);
                  $date = date("m d Y", $x);
                  $split = explode(' ', $date);
                  // the intval casts are optional, i think
                  $m = intval($split[0]);
                  $d = intval($split[1]);
                  $y = intval($split[2]);
                  $valid = checkdate($m, $d, $y);
                  echo $valid;```

            There is also date_parse which returns an array of what it finds, including a warning_count and error_count which will be >0 if there are any warnings or errors about what it's given.

            But like NogDog points out: if you're formatting a Unix timestamp as a date, you're going to get a valid date.

              Write a Reply...