The date_create() function of PHP is very useful when we want to create an arbitrary custom date. The date() function of PHP is for the current date and time but date_create() used to create any custom date.
Whereby we can know the day name on a particular date, was that a leap year, months name, days and weeks number of that year, etc.
This function returns an object. Now, we have to use the format() to tell it the format of the date we used in date_create(). Without applying format on date_create() we can't print that date we defined in the argument of the date_create() function.
You will understand it better by some example. So lets dive into examples.
date_create() function examples
Print custom date
<?php
$c_date = date_create("04-04-2020");
$c_date_result = $c_date->format("d-m-y");
echo $c_date_result;
?>
***We use this object operator ( -> ) when an object also has a collection of classes as well as properties and methods in it.
Print only day from custom date
<?php
$c_date = date_create("04-04-2020");
$date_format = $c_date->format("d-m-y"); //define format
$only_day = $c_date->format("d"); //get only day
echo $only_day;
?>
Print only month from custom date
Print month (numeric) Ex- 04 (Use 'm' character) and 'j' when you print only 4.
<?php
$c_date = date_create("04-04-2020");
$date_format = $c_date->format("d-m-y");
$only_month = $c_date->format("m");
echo $only_month;
?>
Print month name in short (ex- Apr) [Use capital 'M' character]
<?php
$c_date = date_create("04-04-2020");
$date_format = $c_date->format("d-m-y");
$only_month = $c_date->format("M");
echo $only_month;
?>
Print full name of the month (ex- April) [Use capital 'F' character]
<?php
$c_date = date_create("04-04-2020");
$date_format = $c_date->format("d-m-y");
$only_month = $c_date->format("F");
echo $only_month;
?>
Print only year from custom date
use small "y" for last two digits of year and use capital "Y" for all four digits of the year.
Two digit year (ex- 20)
<?php
$c_date = date_create("04-04-2020");
$date_format = $c_date->format("d-m-y");
$only_year = $c_date->format("y");
echo $only_year;
?>
Four digit year (ex- 2020)
<?php
$c_date = date_create("04-04-2020");
$date_format = $c_date->format("d-m-y");
$only_year = $c_date->format("Y");
echo $only_year;
?>
Get the day of week from a date in PHP using date_create() function
- To print the first three letters of the day, you have to use a capital 'D' character in the format() as a string.
- For the full name of the day of the week, then pass small 'l' in the argument of the format() method.
Short name of day (ex: Sun)
<?php
$c_date = date_create("05-04-2020");
$date_format = $c_date->format("d-m-y");
$day_of_week = $c_date->format("D");
echo $day_of_week;
?>
Full name of the day (ex: Sunday)
<?php
$c_date = date_create("05-04-2020");
$date_format = $c_date->format("d-m-y");
$day_of_week = $c_date->format("l");
echo $day_of_week;
?>
Also, take a look on this code
<?php
$c_date = date_create("05-04-2020");
$date_format = $c_date->format("d-m-y");
$date = $c_date->format("l, d-m (F)-Y");
echo $date;
?>
Publish A Comment