Everything You Need to Know About Date format in PHP

Today, We want to share with you Everything You Need to Know About Date format in PHP.In this post we will show you how to change date format in php?, hear for php date format from variable we will give you demo and example for implement.In this post, we will learn about date with an example.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

All About Date format in PHP

There are the Following The simple About how to set date format in php Full Information With Example and source code.

As I will cover this Post with live Working example to develop how to change format of date in php , so the timeanddate is used for this example is following below.

Everything You Need to Know About Date format in PHP
Everything You Need to Know About Date format in PHP

This Post uses PHP strtotime() as well as date() inbuilt functions to convert date time format. For demo with All about Date Related Solution, you have saved or added a date such as a YYYY-MM-DD format in a variable as well as We should to change this format to MM-DD-YYYY format.

We can simple step to step this by converting date format so first to seconds using simple strtotime() function. and then re-construct change the date to user accordning to get format using date() function. Below is some examples of date format:

Definition & Usage

in simplle words date_format() inbuild methods returns a date user freindly formatted uniq or relavnt to the give your specified format.

todays date in PHP

<?php 
echo "Today's date is :"; 
$todays_date = date("d/m/Y"); 
echo $todays_date; 
?> 

Change YYYY-MM-DD => MM-DD-YYYY

And then I have date yyyy-mm-dd (“2022-04-18”) format as well as some converting it to mm-dd-yyyy (“04-18-2022”) format.

$myDate = "2022-04-18";
 
$freshDate = date("m-d-Y", strtotime($myDate));
echo $freshDate;

Change YYYY-MM-DD => DD-MM-YYYY

Now, I have date yyyy-mm-dd (2021-04-22”) format and converting it to dd-mm-yyyy (“22-04-2021”) format.

$myDate = "2021-04-22";
 
$freshDate = date("d-m-Y", strtotime($myDate));
echo $freshDate;

Change DD/MM/YYYY => YYYY-MM-DD

If you have slashes in date format such as a “22/04/2021” and need to convert / with hyphens (-). The simple demo will help you to convert DD/MM/YYYY (“22/04/2021”) to YYYY-MM-DD (2021-04-22).

$myDate = "22/04/2021";
 
$date = str_replace('/', '-', $myDate );
$freshDate = date("Y-m-d", strtotime($date));
echo $freshDate;

PHP date_format() Function

how to convert date format in php?

Return a simple a new DateTime object, after that format date php:

Example of PHP Date Convert

<!DOCTYPE html>
<html>
<body>

<?php
$live_date=date_create("2021-03-15");
echo date_format($live_date,"Y/m/d H:i:s");
?>

</body>
</html>

results:

2021/03/15 00:00:00

Convert a date format in PHP

Use strtotime() and date():

You can easyly to convert a date from yyyy-mm-dd to dd-mm-yyyy

$source_date = "2021-04-25";
$fresh_date = date("d-m-Y", strtotime($source_date));
echo $fresh_date;

Working With Datetime In PHP Examples

you can also See the PHP strtotime as well as date documentation on the PHP Main web site. (Difference Between Two Dates In Years, Months, Days In PHP)

Formatting the Current Date and Time in PHP

How To Convert Date String To Date PHP

get the time and date, present year, present month, present hour, etc.

<?php
 
// Results — 2021
echo date('Y');
 
// Results — September 2021
echo date('F Y');
 
// Results — 25 September, 2021
echo date('d F, Y');
 
// Results — 25 September, 2021 (Thursday)
echo date('d F, Y (l)');
<br>

How To Convert Date Format In PHP?

Character Meaning Example
d day of the month with leading zeros 03 or 17
j day of the month without leading zeros 3 or 17
D day of the week as a three-letter abbreviation Mon
l full day of the week Monday
m month as a number with leading zeros 09 or 12
n month as a number without leading zeros 9 or 12
M month as a three-letter abbreviation Sep
F full month September
y two-digit year 18
Y full year 2018

PHP Date and Time

The PHP simple main date() inbuild function is used to format a set date format and/or a get the time time and date or timestemps.

The PHP date() inbuild function user get the any formats a timestamp to a good way to read and understanding date and time.

  • The PHP Date() Function
  • Get a Date
  • PHP Tip – Automatic Copyright Year
  • Get Your Time Zone
  • Make a Date With mktime()
  • Make a Date From a String With strtotime()
  • Get the Unix Timestamp
  • Adding, Subtracting and Comparing Dates

Get a Date

<?php
echo "Today is " . date("Y/m/d") . "<br>";
echo "Today is " . date("Y.m.d") . "<br>";
echo "Today is " . date("Y-m-d") . "<br>";
echo "Today is " . date("l");
?>

Automatic Copyright Year using date

© 1992-<?php echo date("Y");?>

//© 1992-2020

Get a Time

<?php
echo "The current time is " . date("h:i:sa");
?>

Get Your Time Zone

<?php
date_default_timezone_set("America/New_York");
echo "Your Time Zone is " . date("h:i:sa");
?>

Date With mktime()

<?php
$d=mktime(10, 12, 23, 7, 11, 2021);
echo "Make your date is " . date("Y-m-d h:i:sa", $d);
?>

Date From a String With strtotime()

<?php
$d=strtotime("11:30pm April 15 2022");
echo "Date From a String With strtotime() is " . date("Y-m-d h:i:sa", $d);
?>

Get the Unix Timestamp

<?php
 
$twent_days_later = time() + 20*60*60*24;
// Results — It will be Sunday 20 days later.
echo 'It will be '.date('l', $twent_days_later).' 20 days later.';
 
$twent_days_ago = time() - 20*60*60*24;
// Results — It was Monday 20 days ago.
echo 'It was '.date('l', $twent_days_ago).' 20 days ago.';
 
?>

Advanced Date format Tutorials

Adding, Subtracting and Comparing Dates

<?php
 
$present = date_create('now');
$future = date_create('last day of January 2024');
$interval = date_diff($present, $future);

echo $interval->format('%Y years, %M months and %d days');
 
$present = new DateTime('now');
$future = new DateTime('last day of January 2024');
$interval = $present->diff($future);

echo $interval->format('%Y years, %M months and %d days');
 
?>

<?php
 
$c_date = new DateTime('now');
$timestemp_counter = new DateInterval('P20Y5M20D');
$c_date->add($timestemp_counter);
 
echo 'Pakainfo will be '.$c_date->format('l, d F, Y').' after '.$timestemp_counter->format("%Y years, %M months and %d days").' from today.';
 
$c_date = date_create('now');
$timestemp_counter = date_interval_create_from_date_string('20 years 05 months 20 days');
date_add($c_date, $timestemp_counter);
 
echo 'Pakainfo will be '.$c_date->format('l, d F, Y').' after '.$timestemp_counter->format("%Y years, %M months and %d days").' from today.';
 
?>

<?php
 
$c_date = new DateTime('today');
$eveny_famous = new DateTime('25 December 2025');
 
while($c_date > $eveny_famous) {
    $eveny_famous->add(new DateInterval('P1Y'));
}
 
if($c_date < $eveny_famous) {
    $interval = $c_date->diff($eveny_famous);
    echo $interval->format('%a days').' until Diwali!';
}
 
if($c_date == $eveny_famous) {
    echo 'Diwali :)';
}

?>

Web Programming Tutorials Example with Demo

Read :

Summary

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

I hope you get an idea about how to set date format in php.
I would like to have feedback on my infinityknow.com 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.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Browser window resize function with AngularJS
  2.   Laravel Increase Website Speed performance optimization
  3.   Top 10 Ways to Increase Google Adsense Earnings
  4.   WooCommerce Get Product Images
  5.   Laravel WhereHas then orWhereHas Example
  6.   jQuery Convert string to integer Example
  7.   AngularJS Fancybox Popup Simple Example
  8.   Vuejs settimeout and setinterval components Example
  9.   Laravel 6 Image Uploader and Browser for CKEditor
  10.   AngularJS Simple calculator web Application

Leave a Reply

Your email address will not be published. Required fields are marked *