Skip to content
Advertisement

Using DateTime as a one-liner in PHP

A short and simple question which I couldn’t really find an answer for.

In procedural PHP I could echo a date

JavaScript

In Object oriented PHP I have to use two lines

JavaScript

Is it possible to do this in one line?

Advertisement

Answer

There are two options:

  1. Create a DateTime instance using parenthesis and format the result:

    JavaScript
  2. You can use the date_create function:

    JavaScript

Live Example: https://3v4l.org/fllco

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement