Current date and time can get using PHP function.
User registration time or want to know the user last login time, booking date or time for order product etc all are possible to get by date() function in PHP.
Date function syntax
1 2 3 | <?php echo date('D d-m-y h:i:s a'); ?> |
Output
1 | Sun 08-07-18 10:17:05 pm |
Explanation
date() function has many different parameters. All different parameters have different meaning. Like in above example
D is return today’s day value in first three letters
d is return today’s date value with leading zero.
m is return current month value in numeric format with leading zero.
y is return current year value only last two digit.
h is return current hours value with leading zero in 12 hour format.
i is return current minute value with leading zero.
s is return current second value with leading zero.
a is return am or pm value of time in 12 hours format.
Note: date() function always return machine time. If machine time is set wrong then its return wrong time and date. And also it always returns to Europe/Berlin the time. Because in server setting file php.ini file have set date.timezone=Europe/Berlin.
Set current timezone then in PHP have a function to set current location time zone for particular one application. If anyone want permanently changed timezone the set date.timezone = Asia/Kolkata(or any preferred location) in php.ini file and restart the xampp or wampp.
date_default_timezone_set():
date_default_timezone_set() this function is used to set particular timezone for particular one program where it is run.
Example:
1 2 3 4 | <?php date_default_timezone_set("Asia/Kolkata"); // its only return Kolkata’s current date and time. echo date('D d-m-y h:i:s a'); ?> |
Output:
1 | Mon 09-07-18 01:50:57 am |
Date function has any other parameter to return date and time value in different format.
[table id=11 /]
Time function in PHP
time() function is return total time value in the number of second. The Time calculate from 1st January 1970,
1 2 3 | <?php echo time(); ?> |
Output:
1 | 1531085559 |
Return date and time using time() function
Return current time as a UNIX timestamp.
Example
1 2 3 4 | <?php date_default_timezone_set("Asia/Kolkata"); echo date('D d-m-y h:i:s a',time()); ?> |
Output:
1 | Mon 09-07-18 03:16:52 am |
strtotime() in PHP
strtotime() function convert English textual date-time value to a UNIX timestamp. Using this function can return any past or future time or date value.
1 2 3 4 5 6 7 8 9 | <?php date_default_timezone_set("Asia/Kolkata"); //What Day was it on 15th August, 1947? echo date("D", strtotime("15 August 1947") ); $date = date("d-m-Y"); $next_date = date('d-m-Y', strtotime($date .' +1 day')); echo "<BR>".$next_date; ?> |
Output
1 2 | Fri 10-07-2018 |
date_diff() function in PH
The date_diff() is return the difference between to date.
1 2 3 4 5 6 | <?php $date1=date_create("2005-01-01"); $date2=date_create("2007-03-07"); $diff=date_diff($date2,$date1); echo $diff->format("%R%a days"); ?> |
Output:
1 | +795 days |
date_create() function return new DateTime object.
%R return + or – sign to denote the difference.
%a returns actual result.