Ask Your Question
2

How to modify the format of the year in PHP?

asked 2021-11-06 11:00:00 +0000

djk gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-08-29 12:00:00 +0000

lalupa gravatar image

To modify the format of the year in PHP, we can use the date() function which returns the formatted date. The date() function accepts two parameters:

  1. The first parameter is a format string that defines the format of the output.
  2. The second parameter is an optional timestamp, which defaults to the current time if not specified.

Here are some examples of modifying the format of the year:

  1. Changing the year format from "Y" to "y":

$originaldate = "2019-05-15"; $newdate = date("y-m-d", strtotime($original_date)); // Output: 19-05-15

  1. Converting the year format from "Y-m-d" to "d-m-Y":

$originaldate = "2019-05-15"; $newdate = date("d-m-Y", strtotime($original_date)); // Output: 15-05-2019

  1. Removing the century from the year format:

$originaldate = "2019-05-15"; $newdate = date("y-m-d", strtotime($original_date)); // Output: 19-05-15

  1. Adding the ordinal suffix to the day:

$originaldate = "2019-05-15"; $day = date("jS", strtotime($originaldate)); // Output: 15th

By using the date function with arguments as per the requirement, we can modify the format of the year.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-11-06 11:00:00 +0000

Seen: 9 times

Last updated: Aug 29 '22