PHP: Date and Time Function

PHP: Date and Time Function

June 8, 2020

Every programming have inbuilt function or Object which handles date and time. PHP also have a date and time function. let’s see how it’s work.

date() function

The date() function formats a local date and time, and returns the formatted date string. The date function should have at least one parameter.

date(format, timestamp)

 

PHP microtime function returns current Unix timestamp with microseconds.

microtime(bool $as_float = false): string|float

1 microseconds = 0.001 milliseconds

1000 microseconds = 1 milliseconds

$startTime = microtime(true); //get time in micro seconds
usleep(100); 
$endTime = microtime(true);
echo "Milliseconds to execute:". ($endTime-$startTime)*1000;

 

How to check for the 1st day of an year in php

<?php
  if ( date('z') === '0' ) {
   echo "Today is the first day of the year.";
  }
?>

z – The day of the year (from 0 through 365)

Source: https://stackoverflow.com/questions/12001467/how-to-check-for-the-1st-day-of-an-year-in-php

 

How to get time difference in minutes in PHP?

<?php
$to_time = strtotime("2008-12-13 10:27:00");
$from_time = strtotime("2008-12-13 10:21:00");
echo round(abs($to_time - $from_time) / 60,2). " minute";
?>

Leave A Comment