Skip to main content

Working with datetime in PHP Examples

Today, We want to share with you Working with datetime in PHP Examples.In this post we will show you PHP Date & Time Function, hear for DateTime PHP: Compare, Format and Create any Dates we will give you demo and example for implement.In this post, we will learn about get date from datetime object php with an example.

Working with datetime in PHP Examples

There are the Following The simple About Working with datetime in PHP Examples Full Information With Example and source code.

As I will cover this Post with live Working example to develop PHP Date and Time Functions, so the Working with Mutable and Immutable DateTime in PHP for this example is following below.

Also Read :  Great UNIX Commands Tutorial for Beginners

Working with date in PHP

Method 1: PHP date() Function

echo "Today's date is :"; 
$today = date("d/m/Y"); 
echo $today; 

Method 2: date() function now

Formatting the Current Date and Time in PHP

// Output — 2020
echo date('Y');
// Output — September 2020
echo date('F Y');
// Output — 13 September, 2020
echo date('d F, Y');
// Output — 13 September, 2020 (Thursday)
echo date('d F, Y (l)');

Method 3: Get the Unix Timestamp

$ten_days_later = time() + 10*60*60*24;
// Output — It will be Sunday 10 days later.
echo 'Good Luck It will be'.date('l', $ten_days_later).' 10 days later.';
$ten_days_ago = time() - 10*60*60*24;
// Output — It was Monday 10 days ago.
echo 'Good Luck It was'.date('l', $ten_days_ago).' 10 days ago.';

Method 4: Dates Examples

Also Read :  Insert update delete Using Laravel 5.2 Ajax CRUD laravel without refresh page

PHP Adding, Subtracting and Comparing Dates

$current = date_create('now');
$nextdt = date_create('last day of January 2029');
$baktm = date_diff($current, $nextdt);
// Results — 05 years, 04 months and 17 days
echo $baktm->format('%Y years, %M months and %d days');
$current = new DateTime('now');
$nextdt = new DateTime('last day of January 2029');
$baktm = $current->diff($nextdt);
// Results — 05 years, 04 months and 17 days
echo $baktm->format('%Y years, %M months and %d days');

Method 5: Converting a Time Stamp

Converting a Time Stamp with date()

   print date("m/d/y G.i:s<br>", time());
   print "Good Luck, Today is ";
   print date("j of F Y, \a\t g.i a", time());

Angular 6 CRUD Operations Application Tutorials

Read :


You can also read about AngularJS, ASP.NET, VueJs, PHP.

Also Read :  PHP Date and Time Formatting Functions

I hope you get an idea about Working with datetime in PHP Examples.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Web Developer

Hi, I’m Web developer, PHP is the most popular Programming & Web Development blog. Our mission is to provide the best online resources on programming and web development.Any visitors of this site are free to browse our tutorials, live demos, Examples and download scripts.If you have a project that you want to get started, launch quickly, contact me. Email : [email protected]