Close Menu
My SiteMy Site
  • Home
  • Reviews
  • Computers
  • How Tos
  • Travel & Lifestyle
  • Phone ROMs
  • Contact
    • Advertise
    • About Us
Facebook X (Twitter) Instagram
My SiteMy Site
  • Home
  • Reviews
  • Computers
  • How Tos
  • Travel & Lifestyle
  • Phone ROMs
  • Contact
    • Advertise
    • About Us
My SiteMy Site
Home»Programming»PHP»How To Print/Echo a DateTime Object In PHP
PHP

How To Print/Echo a DateTime Object In PHP

Oscar FrankBy Oscar Frank2 Mins Read
Share
Facebook Twitter LinkedIn WhatsApp Pinterest Email

I decided to write this short PHP tutorial on how to print a Date Time object because this is one of the frequent questions I get from people who are new with the language. Trying to print or echo a DateTime object the regular way you’d have done a variable or trying to loop through it thinking of it as an array object would not work. To get it printed out, you’d have to go a different route.
How To Echo/Print a Date Time Object in PHP
In order to display the date from a Date Time Object in PHP, you do not have to use the echo statement, as it would not return the date from the object. You have to make use of the date-format method ( date_format(); ) and below is the syntax of this method/function.

Date Format Syntax

[pastacode lang=”php” manual=”date_format(object%2Cformat)%3B” message=”php date format syntax” highlight=”” provider=”manual”/]

As can be seen, this method accepts two arguments. The first one being the object (date time object) to be printed and the second refers to the format it would be display.

The DateTime object could be a date saved and queried from say a MySQL Database (must have been created using the date_create() method), while the format argument accepts formatting strings that determines how the date and time would be displayed. Take the below code as an example.

[pastacode lang=”php” manual=”echo%20date_format(%24date%2C%22Y%20-%20m%20-%20d%20H%3Ai%3As%22)%3B” message=”Echo a php date_time object” highlight=”” provider=”manual”/]


Code Explanation

$date – Refers to the dateTime object created or queried from a DB.

Y – Refers to the year

m – Refers to the month

d – Refers to the day

H – Refers to the hour

i – Refers to the minute

s – Refers to the second.

Thus if the time/date pulled or created earlier was for the 3rd of January 2017 by 8:22 am, with the above formatting, the date time would be printed thus

2017 – 11 – 03 08:22:00

There are a lot of ways you might want to format your date time object and here is a list of other characters that can be used for formatting.

Note that the two arguments or parameters as you might call it are required in the Date Format method.

DateTime Object php programming Tutorials

Related Posts

Common Mistakes Newbie Developers Make and Solutions

March 23, 2021

7 Best Laptops For Coders In 2025

February 28, 2020

How To Set Up Google Family Link

February 13, 2020
View 2 Comments

2 Comments

  1. Prince Menaha on January 4, 2017 1:45 am

    Very good. Thanks a lot Oscar.
    Please I would like to interview you on my blog. Hope you don’t mind?

    Reply
    • Oscar Frank on January 4, 2017 4:26 am

      Hi Prince,
      Thanks for dropping by. Sure I don’t mind. 🙂 . You can reach me via the contact page.

      Reply
Leave A Reply Cancel Reply

Recent Posts
  • 6 Sports Apps You Must Have On Your Phone
  • Valencia Travel Guide: All You Need to Know About This Spanish Gem
  • TikTok Faces Unexpected Ban in Senegal, Sparks Global Curiosity
  • The Apple GPT & Apple’s Chatbot Mysterious Debut
  • Triller Takes on TikTok: Filing for Public Listing
Get Exclusive Tech Insights!
My Site
Facebook X (Twitter) Instagram YouTube
© 2025 Oscarmini Co. Designed by illBytes.

Type above and press Enter to search. Press Esc to cancel.