Sobes.tech
Junior

What date and time functions do you know?

sobes.tech AI

Answer from AI

  • date(): Formats local date and time.
  • strtotime(): Converts a date/time string to a Unix timestamp.
  • mktime(): Returns the Unix timestamp for a given date.
  • time(): Returns the current Unix timestamp.
  • getdate(): Returns an associative array of date and time information.
  • idate(): Formats local time/date as an integer.
  • gmdate(): Formats date/time in Greenwich Mean Time based on Unix timestamp.
  • gmstrftime(): Formats Greenwich Mean Time date/time according to locale settings.
  • setDate(): Sets the date for a DateTime object.
  • setTime(): Sets the time for a DateTime object.
  • setTimestamp(): Sets the Unix timestamp for a DateTime object.
  • add(): Adds a DateInterval to a DateTime object.
  • sub(): Subtracts a DateInterval from a DateTime object.
  • diff(): Returns the difference (DateInterval) between two DateTime objects.

I also actively use classes DateTime, DateTimeImmutable, and DateInterval for more object-oriented work with dates and times. For example:

<?php
// Creating a DateTime object
date = new DateTime('now');
echo $date->format('Y-m-d H:i:s');

// Adding an interval
$interval = new DateInterval('P1D'); // Add 1 day
$date->add($interval);
echo $date->format('Y-m-d H:i:s');
What date and time functions do you know? — PHP - sobes.tech