PHP | IntlCalendar toDateTime() Function

The IntlCalendar::toDateTime() function is in 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 then 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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);
  
?>

chevron_right


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

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.