Date Anjn
Date Anjn
Date Anjn
UNIVERSITY
COLLEGE OF ELECTRICAL AND MECHANICAL
ENGINEERING DEPARTMENT OF SOFTWARE
ENGINEERING
COURSE: INTERNET PROGRAMMING II C
OURSE CODE: SWEG3102
DATE AND TIME
SECTION: B
GROUP 1
AGENDA
D a t e f u n c ti o n
T i m e f u n c ti o n
C r e a ti n g d a t e s f r o m s t r i n g s
Time zones
A d d i ti o n a l f u n c ti o n s
I n t e g r a ti o n a n d u s e c a s e s
2
DATE FUNCTION
date(format, timestamp)
3
Y O U C A N U S E VA R I O U S F O R M AT
CODES WITHIN THE STRING TO
C U S T O M I Z E T H E O U T P U T.
C O M M O N E X A M P L E S :
4
TIME
FUNCTION
T I M E ( )
This function returns the current Unix timestamp like (1715501730). Unix timestamp is the number of
seconds that have passed since the Unix Epoch, which is January 1, 1970 at 00:00:00 GMT.
Unix timestamps are useful for tracking and sorting dated information in distributed applications because
they are interpreted the same regardless of region and are calculated from the same point in time
regardless of time zone.
5
CREATING DATES FROM STRING
strtotime() function
This function converts a string representing a date and time into a Unix timestamp.
Example: $dateString = "2024-05-12";
$timestamp = strtotime($dateString);
DateTime Class:
While date() and strtotime() are handy, the DateTime class (available since PHP 5.2) offers a
more robust approach. It provides object-oriented methods for intricate date and time handling.
$dateObj = new DateTime();
echo $dateObj->format('Y-m-d H:i:s'); // Output: Current date and time
// Modifying the date
$dateObj->modify('+1 day'); // Add one day
echo $dateObj->format('Y-m-d'); // Output: Tomorrow's date
// Date arithmetic with intervals
$interval = new DateInterval('P10D'); // Create an interval of 10 days
$dateObj->add($interval); //add 10 days
6
DateInterval Class:
This class represents a duration between two dates. You can use it to perform
calculations involving time differences.
This class is similar to DateTime but represents an immutable date and time. This means
you cannot directly modify the object's 3 date or time. Instead, you create new objects
with the desired changes. It enforces better practices for data integrity.
mktime() function:
This function allows you to create a Unix timestamp from individual date and time
components. You can specify the year, month, day, hour, minute, and second.
D AT E A N D T I M E
TIME ZONES
D AT E A N D T I M E
8
ADDITIONAL FUNCTIONS
checkdate(): Validates a Gregorian calendar date.
checkdate(month, day, year) accepts parameter in int format.
idate(): Formats a local time/date part as an integer.
idate(format, timestamp(optinal))
getdate(): Returns an associative array containing date and time information.
getdate(timestamp(optional))
When called without any arguments, it returns an associative array containing various components of
the current date and time in the server's local time zone.
gmdate(): Formats a GMT/UTC date and time.
gmdate(format, timestamp)
echo gmdate("l jS \of F Y h:i:s A")
Sunday 12th of May 2024 09:38:13 AM
gmmktime(): Creates a Unix timestamp for a GMT/UTC date and time. Then use it to find the day of that date:
gmmktime(hour, minute, second, month, day, year)
microtime(): Returns the current time in microseconds.
no parameter
9
INTEGRATION AND USE
CASES
some common use cases for PHP date and time
functions:
• Displaying the current date and time on a web page
• Logging timestamps in application logs
• Calculating time differences between events
• Scheduling tasks to run at specific times
• Setting expiration dates for cookies or sessions
10
Reference
• PHP Documentation on Date and Time Functions: PHP: DateTime - Manual.
(n.d.). https://www.php.net/manual/en/class.datetime.php
• A comprehensive tutorial on PHP Date and Time: PHP date and time. (n.d.).
https://www.w3schools.com/php/php_date.asp
Group 1
THANK YOU
Group members ID.NO
12