EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

PHP Change Date Format

Home » Software Development » Software Development Tutorials » PHP Tutorial » PHP Change Date Format

PHP Change Date Format

Introduction to PHP Change Date Format

PHP Date functions are used to print the date of the server or the current date. It is mostly used in logging the information when the system is performing any operations on the server. One can change the date information about how it is required for any particular operation. The data provided will be standard (yyyy-mm-dd) as the date can be changed as per the need using many date methods and functions. Below listed are the techniques used to change the date format in the server.

PHP Date Format:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

PHP Change Date Format provides you the date information of the server where the particular PHP script is executed. So the server will provide us the information in the standard format when the date function gets executed. If you want to change the format as you wish it can be done using several ways.

Types of Date Formats

There are few data formats used in PHP to print the date as per the requirement:

  • date_create(): This function will create the date as per the mentioned date format in the date_format function. If you are passing the date format as dd-mm-yyyy it will print the date in the format that is mentioned. It returns the object to date_format () function.
  • date_modify(): This function will modify the date specified by the user i.e. if the user is specifying +15 days then the date printed will be 15 days ahead of the current date. This function modifies the timestamp of the server to be printed.
  • date_create_from_format (): This function will create the date mentioned in the format and creates an object according to the mentioned format and passed to the date_format function.

How to Convert Date Format?

Many programmers use different date and time functions to make the changes as per their requirement. The change depends upon the region and based on where it is used as well.

For example, if the user wants to use the database for any employee entry and the date format of the server is different from the format used by the database, then the database will not accept the standard format as the database has its restrictions. So the user has to change the format to the required ones using various techniques available in PHP. There are many instances where the date and time functions have their format.

For example, in India, the date will be printed in IST (Indian Standard Time) format, in the USA it will be printed in CDT/CST (Central Standard Time) format, in Japan it will be printed in JST (Japan Standard Time) format, in Canada it will be printed in EST (Eastern Standard Time). So, the database and the server will have different time zones comparatively. To get it compatible with the date format, time zones changes accordingly for not getting any conflicts between the data to be entered. One of the techniques is strtotime() function which prints the date mentioned in the format as it is. 

Popular Course in this category
PHP Training (5 Courses, 3 Project)5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,684 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

Example #1

Here is an example to use strtotime() function which prints the date.

Code:

<!DOCTYPE html>
<html>
<body>
<?php
$o = "2019-10-30";
// It creates the timestamp from the date mentioned.
$a = strtotime($o);
$new = date("d/m/Y", $a);
echo $new;
?>
</body>
</html>

Output:

PHP Change Date Format 1

In the above output, the date mentioned will be the one that is mentioned in the date function. As the date mentioned here is d/m/y the output also has the same format. If you want to specify a hyphen(-) instead of backslash(/) you can mention in the date format itself.

Example #2

Here are a few examples of how to convert the date from the standard one to the required format.

Code:

<!DOCTYPE html>
<html>
<body>
<?php
$currentdate=date_create("2012-09-13");
echo date_format($currentdate,"m/d/Y");
?>
</body>
</html>

Output:

PHP Change Date Format 2

Here in the above code and output, the date specified in the date format function is the one to be printed in the output. So the user can specify in whichever format he wants to see like dd-mm-yyyy or dd-mm-yy or dd/mm/yyyy or dd/mm/yy, etc.

Code:

<!DOCTYPE html>
<html>
<body>
<?php
$a = '2019-05-25';
$d = new DateTime($a);
echo $d->format('Y.m.d');
?>
</body>
</html>

Output:

Output 3

In the above code and the respective output, the date format is mentioned in the code passed with the function format as seen above. The DateTime() method will convert the date in the format mentioned in the format function and will create an object to pass it to the format function.

Code:

<!DOCTYPE html>
<html>
<body>
<?php
$date=date_create_from_format("j-M-Y","27-Feb-2019");
echo " The changed date format is ", date_format($date,"d/m/Y");
?>
</body>
</html>

Output:

Output 4

In the above code, the format specified in the date created from the format method is the input to the method date_format as the date mentioned in the code will be printed in the format mentioned in the code.

So we learned many methods as how to change the date for a particular format using various date functions. In all the date functions the common thing was already the date was mentioned in the code itself. If you want to take the current date as the input to the specified format you can simply use date () function or Date Time () function to retrieve the date and time respectively. So the user can work the static way as well as a dynamic way to retrieve the date from the server. Similarly, like date functions, we have various time functions that can be used to change the time zones as well as the time format of the servers. All of the servers will have a common time i.e. UTC (Universal Time Zone) unless it gets changed to its respective country/region. 

Conclusion – PHP Change Date Format

In this article, we discussed the date format, how to change the date format and also its types. The main objective of these changes is to have a smooth flow between various conflicts faced when servers of different countries are used together and have a commonplace as a repository.

Recommended Article

This is a guide to PHP Change Date Format. Here we discuss different types of date format and its examples along with code implementation and output. You can also go through our other suggested articles to learn more –

  1. Palindrome in PHP
  2. Abstract Class in PHP
  3. Socket Programming in PHP
  4. Factorial in PHP

PHP Training (5 Courses, 3 Project)

5 Online Courses

3 Hands-on Project

28+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PHP Tutorial
  • Advanced
    • Overloading in PHP
    • Overriding in PHP
    • Method Overloading in PHP
    • Inheritance in PHP
    • Multiple Inheritance in PHP
    • PHP Interface
    • Encapsulation in PHP
    • PHP Constants
    • PHP Magic Constants
    • PHP Regular Expressions
    • PHP GET Method
    • PHP Annotations
    • PHP Encryption
    • PHP file Functions
    • PHP readfile
    • PHP?Write File
    • PHP Append File
    • PHP Type Hinting
    • PHP Filters
    • PHP Float
    • PHP Form
    • PHP Form Validation
    • Sorting in PHP
    • PHP usort()
    • PHP Stack Trace
    • PHP Stack Overflow
    • PHP Pagination
    • PHP implode
    • Polymorphism in PHP
    • Abstract Class in PHP
    • PHP Final Class
    • PHP Custom Exception
    • error_reporting() in PHP
    • PHP Log Errors
    • Access Modifiers in PHP
    • PHP Change Date Format
    • Static Method in PHP
    • PHP File Handling
    • PHP Output Buffering
    • Get IP Address in PHP
    • Upload a File in PHP
    • String in PHP
    • Public Function in PHP
    • Private in PHP
    • Protected in PHP
    • basename in PHP
    • Validation in PHP
    • PHP mail()
    • PHP Email Form
    • PHP Directory
    • PHP Create Session
    • PHP include_once
    • PHP json_decode
    • PHP XMLWriter
    • PHP XML Reader
    • PHP XML Parser
    • PHP XML into Array
    • Phalcon Framework
  • PHP Basic
    • Introduction To PHP
    • What is PHP
    • PHP Keywords
    • Advantages of PHP
    • Career In PHP
    • Comments in PHP
    • PHP Commands
    • PHP Frameworks
    • PHP Compiler
    • Variables in PHP
    • PHP Superglobal Variables
    • PHP Versions
    • Object in PHP
    • What is Drupal
    • Top PHP Frameworks
    • WebStorm IDE
    • What is phpMyAdmin?
    • PhpStorm
    • Install phpMyAdmin
    • Phalcon Model
  • Data Types
    • PHP Data Types
    • PHP Integer
    • PHP Booleans
  • Operators
    • PHP Operators
    • Arithmetic Operators in PHP
    • Comparison Operators in PHP
    • Logical Operators in PHP
    • Bitwise Operators in PHP
    • Ternary Operator in PHP
    • PHP String Operators
  • Control Statements
    • Control Statement in PHP
    • PHP if Statement
    • if else Statement in PHP
    • elseif in PHP
    • PHP Switch Statement
    • Continue in PHP
    • Break in PHP
  • Loops
    • PHP Loops
    • For Loop in PHP
    • PHP Do While Loop
    • PHP While Loop
    • While Loop in PHP
    • Foreach Loop in PHP
  • Constructor
    • Constructor in PHP
    • Destructor in PHP
  • State Management
    • Cookie in PHP
    • Sessions in PHP
  • Array
    • What is PHP Array
    • Arrays in PHP
    • 2D Arrays in PHP
    • Associative Array in PHP 
    • Multidimensional Array in PHP
    • Indexed Array in PHP
    • PHP Array Functions
    • PHP unset Array
    • PHP Append Array
    • PHP Array Search
    • PHP Split Array
    • PHP array_push()
    • PHP array_pop()
  • Functions
    • Functions in PHP
    • PHP Math Functions
    • PHP Recursive Function
    • PHP String Functions
    • Hashing Function in PHP
    • Date Function in PHP
    • PHP Anonymous Function
    • Calendar in PHP
    • PHP Call Function
    • PHP Pass by Reference
    • PHP ucfirst()
    • PHP ucwords()
    • trim() in PHP
    • isset() Function in PHP
    • PHP replace
    • PHP fpm
    • PHP strpos
    • preg_match in PHP
    • PHP preg_replace()
    • PHP ob_start()
    • PHP Reflection
    • PHP Split String
    • PHP URL
    • PHP preg_match_all
    • PHP strtoupper()
    • PHP preg_split()
    • PHP substr_replace()
    • PHP setlocale()
    • PHP substr_count()
    • PHP Serialize
    • PHP strlen()
    • PHP async
    • PHP Date Time Functions
    • PHP timezone
    • PHP Data Object
    • print_r() in PHP
    • PHP header()
    • PHP strip_tags()
    • PHP chop()
    • PHP MD5()
    • PHP unset()
    • PHP crypt()
    • PHP wordwrap()
    • PHP is_null()
    • PHP strtok()
    • PHP bin2hex()
    • PHP parse_str()
    • PHP levenshtein()
    • PHP addslashes()
    • PHP strtotime
    • PHP sha1()
    • PHP explode()
    • PHP sscanf()
    • PHP require_once
    • PHP Zip Files
    • PHP $_SERVER
    • PHP $_POST
    • PHP Include and Require
    • PHP POST Method
  • Programs
    • Patterns in PHP
    • Star Patterns in PHP
    • Swapping in PHP
    • Fibonacci Series PHP
    • Factorial in PHP
    • Reverse String in PHP
    • Square Root in PHP
    • Random Number Generator in PHP
    • Palindrome in PHP
    • Prime Numbers in PHP
    • Armstrong Number in PHP
    • Socket Programming in PHP
    • Login Page in PHP
    • PHP Login Template
    • PHP Object to String
  • Database
    • PHP Database Connection
    • How to Connect Database to PHP
  • Interview Questions
    • PHP Interview Questions
    • PHP OOP Interview Questions
    • CakePHP Interview Questions
    • Core PHP Interview Questions

Related Courses

PHP Training Course

Java Servlet Training

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you