Friday, December 27, 2024
Google search engine
HomeLanguagesPHP Function: Date and Time Example

PHP Function: Date and Time Example

PHP Date & Time Function With Example. In this article, we will learn how to use PHP date and time functions. Learn more about PHP date() & PHP time related functions like php strtotime() etc.

PHP Date and Time Function with Examples

In this php date and time example tutorial, we would love to share with you many examples of php date and time functions like, php date to timestamp, php get date, php date format yyyy mm dd, php strtotime, php get month from date, php now, change date format in php, php get year from date, get only date from datetime in php, etc. You can see the examples below:

  • PHP Date() Function
  • PHP Date Function examples
    • php show current date
    • php get current Year
    • PHP get current month
    • PHP get current date and day name
    • PHP day name
    • PHP current day,date,month name, year
    • php get month from date
    • php get last day of month from date
    • php get last date of month from date
    • php get previous month of given date
    • php get next month of given date
    • php get last day and date of given month
    • php get first day of month from date
    • convert current date to timestamp in PHP
    • php get current month number
    • get day name from date in php
    • php get current day of week
    • get the day of the week from a date
  • PHP Time Function

PHP Date() Function

The PHP date() function formats a timestamp to a more readable date and time.

Syntax

date(format,timestamp)

Parameters

Parameter Description
format Required. Specifies the format of the timestamp
timestamp Optional. Specifies a timestamp. Default is the current date and time

Here are some characters that are commonly used for dates:

  • d – The day of the month (from 01 to 31)
  • D – A textual representation of a day (three letters)
  • j – The day of the month without leading zeros (1 to 31)
  • l (lowercase ‘L’) – A full textual representation of a day
  • N – The ISO-8601 numeric representation of a day (1 for Monday, 7 for Sunday)
  • S – The English ordinal suffix for the day of the month (2 characters st, nd, rd or th. Works well with j)
  • w – A numeric representation of the day (0 for Sunday, 6 for Saturday)
  • z – The day of the year (from 0 through 365)
  • W – The ISO-8601 week number of year (weeks starting on Monday)
  • F – A full textual representation of a month (January through December)
  • m – A numeric representation of a month (from 01 to 12)
  • M – A short textual representation of a month (three letters)
  • n – A numeric representation of a month, without leading zeros (1 to 12)
  • t – The number of days in the given month
  • L – Whether it’s a leap year (1 if it is a leap year, 0 otherwise)
  • o – The ISO-8601 year number
  • Y – A four digit representation of a year
  • y – A two digit representation of a year
  • a – Lowercase am or pm
  • A – Uppercase AM or PM
  • B – Swatch Internet time (000 to 999)
  • g – 12-hour format of an hour (1 to 12)
  • G – 24-hour format of an hour (0 to 23)
  • h – 12-hour format of an hour (01 to 12)
  • H – 24-hour format of an hour (00 to 23)
  • i – Minutes with leading zeros (00 to 59)
  • s – Seconds, with leading zeros (00 to 59)
  • u – Microseconds (added in PHP 5.2.2)
  • e – The timezone identifier (Examples: UTC, GMT, Atlantic/Azores)
  • I (capital i) – Whether the date is in daylights savings time (1 if Daylight Savings Time, 0 otherwise)
  • O – Difference to Greenwich time (GMT) in hours (Example: +0100)
  • P – Difference to Greenwich time (GMT) in hours:minutes (added in PHP 5.1.3)
  • T – Timezone abbreviations (Examples: EST, MDT)
  • Z – Timezone offset in seconds. The offset for timezones west of UTC is negative (-43200 to 50400)
  • c – The ISO-8601 date (e.g. 2013-05-05T16:34:42+00:00)
  • r – The RFC 2822 formatted date (e.g. Fri, 12 Apr 2013 12:01:05 +0200)
  • U – The seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)

PHP Date Function Examples

1. php show current date

In this below example, we will use php date function for get the current date in PHP. The format is a current date is Y-m-d.

  • Y = Current Year
  • m = Current Month
  • d = Current Date
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Get Current Date in PHP</title>
</head>
<body>

<?php
// Return current date from the remote server
$t = date('Y-m-d');
echo $t;
?>

</body>
</html>                                		

2. php get current year

If you want to get current of current year date. You can use the php date function like below:

<?php
 echo date('Y');
?>

3. PHP get current month

If you want to get current month from date. You can use the php date function like below.

<?php
echo date('M'). "<br>"; return month name
echo date('m'); // return numeric value 1 to 12
?>

4. PHP get current date and day name

If you want to get current date of day name. You can use the PHP date function like below:

<?php
// Return current date from the remote server
echo date('D'). "<br>";
echo date('d'); 
  
?>

5. PHP day name

If you want to current day name in PHP. You can use the php date function like below:

<?php
// display the day
echo date("l") . "<br>";
?>

6. PHP current day,date,month name, year

If you want to current day, date, month name , year, and time. You can use php date() function like below:

<?php
// display the day, date, month, year, time, AM or PM
echo date("l jS \of F Y h:i:s A");
?>

7. php get month name from date

If you want to get month name from date. You also use php date() function like below:

<?php
// get the month name from the date
echo date("M", strtotime('2019-11-16')) . "<br>";
?>

8. php get last day of month from date

Use the below example for get last day of month from date in php:

<?php


$date = "2019-10-01";

$lastdate = date('Y-m-t',strtotime($date));
$lastday = date('D',strtotime($lastdate));

echo $lastday."<br>";

?>

9. php get last date of month from date

Use the below example for get date of month from date in php:

<?php

$date = "2019-10-01";

$lastdate = date('t',strtotime($date));

echo $lastdate."<br>";

?>

10. php get previous month of given date

if you want to get previous month of given date in. Use the below PHP date() function:

<?php

$date = strtotime('2019-11-16 -1 month');

// php get previous month of given date

echo date('M', strtotime($date))."<br>";
echo date('m', strtotime($date))."<br>";

?>

If you want to get previous month, previous two month, previous three month, four, five month etc from the given date you can easily minues from the given date, like above example.

11. php get next month of given date

if you want to get next month of given date in. Use the below PHP date() function:

<?php

$date = strtotime('2019-11-16 +1 month');

// php get next month of given date
echo date('M', strtotime($date))."<br>";
echo date('m', strtotime($date))."<br>";

?>

If you want to get next month, next two month, next three month, four, five month etc from the given date you can easily minues from the given date, like above example.

12. php get last day and date of given month

php get last day and date from given month. you can use the below example:

<?php


$date = "2019-10-01";

$newDate = date('Y-m-t',strtotime($date));

$lastdate = date('d',strtotime($newDate));
$lastday = date('D',strtotime($newDate));

echo $lastdate."<br>";
echo $lastday."<br>";

?>

13. php get first day of month from date

If you want to get first day of month from date in php. You can use the php date() function like this:

<?php
​
$date = '2019-11-12';
​
// First day of the month.
echo date('Y-m-01', strtotime($date));
​
?>

14. convert current date to timestamp in PHP

Use the below example for convert current date to a timestamp in PHP:

<?php


$date = date('Y-m-d');

$timestamp = strtotime($date);

echo $timestamp."<br>";


?>

15. php get current month number

If you want to get the current month number. use the below example for that:

<?php

echo date('m');

?>

16. get day name from date in php

You want to get day name from date in php. so you can use the below example:

<?php

$date = date('2019-10-15');
//get short name of day
echo date('D',strtotime($date))."<br>";

//get full name of day
echo date('l',strtotime($date));

?>

17. php get current day of week

If you want to get current day of week in php. You can use the below example for that. it will return numeric value.

<?php

$date = date('Y-m-d');
echo date('w',strtotime($date));

?>

18. get the day of the week from a date

you want to get the day of the week from a date. So you can follow the below example for that. it will return numeric value.

<?php

$date = date('2019-10-15');
echo date('w',strtotime($date));

?>

PHP Time() Function

The time() function is a built-in function in PHP which returns the current time measured in the number of seconds since the Unix Epoch.
The number of seconds can be converted to the current date using date() function in PHP. Return current time as Unix timestamp

Syntax

time();

Example 1 of time() function in PHP

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Get Current Time in PHP</title>
</head>
<body>
<?php
$t=time();
echo($t . "<br>");
echo(date("Y-m-d",$t));
?>

</body>
</html>                                		

You may like

  1. Functions: Remove First Character From String PHP
  2. Remove Specific/Special Characters From String In PHP
  3. How to Replace First and Last Character From String PHP
  4. PHP Count Specific Characters in String
  5. Reverse String in PHP
  6. To Remove Elements or Values from Array PHP
  7. PHP remove duplicates from multidimensional array
  8. Remove Duplicate Elements or Values from Array PHP
  9. How to Convert String to Array in PHP
  10. Array Push and POP in PHP | PHP Tutorial
  11. PHP Search Multidimensional Array [key and value and return key]
  12. MySQL DATE() And TIME() Functions (List)
RELATED ARTICLES

Most Popular

Recent Comments