Current.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
  3. use DateTimeImmutable;
  4. use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
  5. class Current
  6. {
  7. /**
  8. * DATENOW.
  9. *
  10. * Returns the current date.
  11. * The NOW function is useful when you need to display the current date and time on a worksheet or
  12. * calculate a value based on the current date and time, and have that value updated each time you
  13. * open the worksheet.
  14. *
  15. * NOTE: When used in a Cell Formula, MS Excel changes the cell format so that it matches the date
  16. * and time format of your regional settings. PhpSpreadsheet does not change cell formatting in this way.
  17. *
  18. * Excel Function:
  19. * TODAY()
  20. *
  21. * @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
  22. * depending on the value of the ReturnDateType flag
  23. */
  24. public static function today()
  25. {
  26. $dti = new DateTimeImmutable();
  27. $dateArray = Helpers::dateParse($dti->format('c'));
  28. return Helpers::dateParseSucceeded($dateArray) ? Helpers::returnIn3FormatsArray($dateArray, true) : ExcelError::VALUE();
  29. }
  30. /**
  31. * DATETIMENOW.
  32. *
  33. * Returns the current date and time.
  34. * The NOW function is useful when you need to display the current date and time on a worksheet or
  35. * calculate a value based on the current date and time, and have that value updated each time you
  36. * open the worksheet.
  37. *
  38. * NOTE: When used in a Cell Formula, MS Excel changes the cell format so that it matches the date
  39. * and time format of your regional settings. PhpSpreadsheet does not change cell formatting in this way.
  40. *
  41. * Excel Function:
  42. * NOW()
  43. *
  44. * @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
  45. * depending on the value of the ReturnDateType flag
  46. */
  47. public static function now()
  48. {
  49. $dti = new DateTimeImmutable();
  50. $dateArray = Helpers::dateParse($dti->format('c'));
  51. return Helpers::dateParseSucceeded($dateArray) ? Helpers::returnIn3FormatsArray($dateArray) : ExcelError::VALUE();
  52. }
  53. }