PHP | IntlCalendar fromDateTime() Function

The IntlCalendar::fromDateTime() function is an inbuilt function in PHP which is used to create an IntlCalendar from a DateTime object or string. The value of new calendar represents the same instant as the DateTime and timezone.

Syntax:

  • Object oriented style
    IntlCalendar IntlCalendar::fromDateTime( mixed $dateTime )
  • Procedural style
    IntlCalendar intlcal_from_date_time( mixed $dateTime )

Parameters: This function accepts single parameter $dateTime which holds the DateTime object or a string that can be passed to DateTime::__construct() function.



Return Value: This function returns the IntlCalendar object on success or NULL on failure. If a string is passed as a parameter then an exception occurs inside the DateTime constructor.

Below programs illustrate the IntlCalendar::fromDateTime() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
  
// Create an IntlCalendar from a DateTime object or string 
$calendar = IntlCalendar::fromDateTime('2019-08-29 09:19:29'); 
  
// Add the date 
$calendar->add(IntlCalendar::FIELD_YEAR, 5); 
  
// Display the result date 
echo IntlDateFormatter::formatObject($calendar), "\n"
  
// Add the date 
$calendar->add(IntlCalendar::FIELD_YEAR, 10); 
  
// Display the result output 
echo IntlDateFormatter::formatObject($calendar), "\n"
  
// Add the date 
$calendar->add(IntlCalendar::FIELD_HOUR_OF_DAY, 10); 
  
// Display the result output 
echo IntlDateFormatter::formatObject($calendar); 
  
?> 

chevron_right


Output:

Aug 29, 2024, 9:19:29 AM
Aug 29, 2034, 9:19:29 AM
Aug 29, 2034, 7:19:29 PM

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
  
// Create an IntlCalendar from a DateTime object or string 
$calendar = IntlCalendar::fromDateTime('2019-08-29 09:19:29'); 
  
// Add the date 
$calendar->add(IntlCalendar::FIELD_MONTH, 1); 
  
// Display the result date 
echo IntlDateFormatter::formatObject($calendar);
  
?> 

chevron_right


Output:

Sep 29, 2019, 9:19:29 AM

Reference: https://www.php.net/manual/en/intlcalendar.fromdatetime.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.