PHP Tutorial
What is PHP Environment Setup of PHP PHP Syntax PHP Comments PHP Echo & print PHP Variables PHP Scope Variables PHP $ and $$ Variable PHP Constants PHP Operators PHP Data TypesPHP Conditional Statements
PHP Loop Type
PHP Arrays
PHP String PHP FunctionsPHP Form Examples
Form Intro Get & Post Form Validation / Required PHP Complete FormPHP Advanced
PHP Include / Require PHP Date & Time PHP Session PHP Cookies PHP File Handling PHP Open File PHP Read File PHP Write File PHP Append File PHP Delete File PHP File Upload PHP Sending Mail PHP Error HandlingPHP Date & Time
In PHP, the date and time functions are used to work with dates and times. Some of the most commonly used functions are:
1) date(): The date() function is used to format a date and time.
The syntax of the function is as follows:
date('Y-m-d');
//Y = Current YEAR
//m = Current Month
//d = Current Date
2) time(): The time() function is used to get the current Unix timestamp. The Unix timestamp is the number of seconds since January 1, 1970 00:00:00 UTC.
For example, to get the current Unix timestamp, you would use the following code:
echo time();
3) strtotime(): The strtotime() function is used to convert a date string to a Unix timestamp.
The syntax of the function is as follows:
echo strtotime("2023-02-19");
DIfferent Type of Date Format:
<?php
// set the default timezone to use.
date_default_timezone_set('Asia/Kolkata');
// 2023-02-20 01:11:09 (the MySQL DATETIME format)
echo date("Y-m-d H:i:s").'<br>';
// 02.20.23
echo date("m.d.y").'<br>';
// 01:11:09
echo date("H:i:s").'<br>';
// Prints something like: Monday
echo date("l").'<br>';
// Prints something like: Monday 20th of February 2023 01:06:39 AM
echo date('l jS \of F Y h:i:s A').'<br>';
// prints something like: Monday the 20th
echo date('l \t\h\e jS').'<br>';
// March 10, 2001, 5:16 pm
echo date("F j, Y, g:i a").'<br>';
// 20, 2, 2023
echo date("j, n, Y").'<br>';
// it is the 20th day.
echo date('\i\t \i\s \t\h\e jS \d\a\y.').'<br>';
// 01:02:09 m is month
echo date('H:m:s \m \i\s\ \m\o\n\t\h').'<br>';
?>
Elevenstech Web Tutorials
Elevenstech Web Tutorials helps you learn coding skills and enhance your skills you want.
As part of Elevenstech's Blog, Elevenstech Web Tutorials contributes to our mission of “helping people learn coding online”.
Read More
Newsletter
Subscribe to get the latest updates from Elevenstech Web Tutorials and stay up to date