What are some common pitfalls to avoid when working with dates and variables in PHP?

One common pitfall when working with dates in PHP is not properly formatting the date before using it in functions like strtotime or date. To avoid this issue, always ensure that the date is in the correct format using the date() function before passing it to other date functions.

// Incorrect way of using a date variable
$date = "2022-01-15";
$timestamp = strtotime($date);

// Correct way of formatting the date before using it
$date = "2022-01-15";
$formatted_date = date("Y-m-d", strtotime($date));
$timestamp = strtotime($formatted_date);