DateType.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. * PHP Version 5.4
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since CakePHP(tm) v 3.0.0
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. namespace Cake\Database\Type;
  18. use Cake\Database\Driver;
  19. use \DateTime;
  20. class DateType extends \Cake\Database\Type {
  21. /**
  22. * Convert DateTime instance into strings.
  23. *
  24. * @param string|Datetime $value The value to convert.
  25. * @param Driver $driver The driver instance to convert with.
  26. * @return string
  27. */
  28. public function toDatabase($value, Driver $driver) {
  29. if (is_string($value)) {
  30. return $value;
  31. }
  32. return $value->format('Y-m-d');
  33. }
  34. /**
  35. * Convert strings into DateTime instances.
  36. *
  37. * @param string $value The value to convert.
  38. * @param Driver $driver The driver instance to convert with.
  39. * @return Datetime
  40. */
  41. public function toPHP($value, Driver $driver) {
  42. if ($value === null) {
  43. return null;
  44. }
  45. return DateTime::createFromFormat('Y-m-d', $value);
  46. }
  47. }