Php Date ( Unix timestamp ) Format Change into day-month-year

This function takes a date string as input and returns the date formatted as "day-month-year".

15

Arun Kr.
09-Jun-24

PHP function named date_format to format dates. This function takes a date string as input and returns the date formatted as "day-month-year". However, there's a slight issue here. When you're returning the formatted date, you're assigning it to a variable $newDate unnecessarily. You can simplify the function like this:

public function date_format($date)
{
    return date("d-M-y", strtotime($date));
}

strtotime() is another PHP function used here. It parses any English textual datetime description into a Unix timestamp.

So, when you call strtotime($date), it converts the input date string into a Unix timestamp, which date() can then format. Finally, the function returns the formatted date string.

@Since 2024 Arun'Log Powered by Arun Git