Tuesday, November 19, 2024
Google search engine
HomeLanguagesPHP | IntlCalendar toDateTime() Function

PHP | IntlCalendar toDateTime() Function

The IntlCalendar::toDateTime() function is an inbuilt function in PHP which is used to convert an IntlCalendar object into a DateTime object. The DateTime object represents upto second precision with error round less than 1 second. 

Syntax:

  • Object oriented style
DateTime IntlCalendar::toDateTime( void )
  • Procedural style
DateTime intlcal_to_date_time( IntlCalendar $cal )

Parameters: This function accepts single parameter $cal which holds the resource of IntlCalendar object. 

Return Value: This function returns a DateTime object with the same timezone as this object and the same time except for the smaller precision on success and returns FALSE on failure. 

Below program illustrates the IntlCalendar::toDateTime() function in PHP: 

Program: 

php




<?php
 
// Set the DateTime zone
ini_set('date.timezone', 'Asia/Calcutta');
ini_set('date.timezone', 'UTC');
 
// Create an instance of IntlCalendar
$calendar = IntlCalendar::createInstance('Asia/Calcutta');
 
// Convert the IntlCalendar into a DateTime object
$datetime = $calendar->toDateTime();
 
// Display the DateTime object
var_dump($datetime);
 
// Declare a IntlGregorianCalendar
$calendar = new IntlGregorianCalendar(2019, 9, 22, 12, 40, 0);
 
// Convert the IntlGregorianCalendar into
// a DateTime object
$datetime = $calendar->toDateTime();
 
// Display the DateTime object
var_dump($datetime);
 
?>


Output:

object(DateTime)#3 (3) {
  ["date"]=>
  string(26) "2019-09-25 11:15:33.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(13) "Asia/Calcutta"
}
object(DateTime)#4 (3) {
  ["date"]=>
  string(26) "2019-10-22 12:40:00.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(3) "UTC"
}

Reference: https://www.php.net/manual/en/intlcalendar.todatetime.php

RELATED ARTICLES

Most Popular

Recent Comments