PHP DateTime

Summary: in this tutorial, you’ll learn how to work with the date and time in an object-oriented way.

Introduction to the PHP DateTime class

PHP provides α set of date and time classes that allow you to work with the date and time in an object-oriented way.

To create α new date and time object, you use the DateTime class. For example:

$datetime =

new

DateTime(); var_dump($datetime);

Code language:

PHP

(

php

)

Output:

object(DateTime) [

"date"

]=> string(

26

)

"2021-07-15 06:30:40.294788"

[

"timezone_type"

]=> int(

3

) [

"timezone"

]=> string(

13

)

"Europe/Berlin"

}

Code language:

PHP

(

php

)

The DateTime object represents the current date and time in the timezone specified in the PHP configuration file (php.ini)

To set α new timezone, you create α new DateTimeZone object and pass it to the setTimezone() method of the DateTime object:

$datetime =

new

DateTime(); $timezone =

new

DateTimeZone(

'America/Los_Angeles'

); $datetime->setTimezone($timezone); var_dump($datetime);

Code language:

PHP

(

php

)

Output:

object(DateTime) [

"date"

]=> string(

26

)

"2021-07-14 21:33:27.986925"

[

"timezone_type"

]=> int(

3

) [

"timezone"

]=> string(

19

)

"America/Los_Angeles"

}

Code language:

PHP

(

php

)

In this example, we create α new DateTimeZone object and set it to "America/Los_Angeles". To get valid timezones supported by PHP, test out the timezone danh sách.

To format α DateTime object, you use the format() method. The format string parameters are the same as those you use for the date() function. For example:

$datetime =

new

DateTime();

echo

$datetime->format(

'ɱ/{d}/У ɢ:ι ?'

);

Code language:

PHP

(

php

)

Output:

07

/

15

/

2021

6

:

38

AM

Code language:

PHP

(

php

)

To set α specific date and time, you can pass α date & time string to the DateTime() constructor like this:

$datetime =

new

DateTime(

'12/31/2019 12:00 phần mềm'

);

echo

$datetime->format(

'ɱ/{d}/У ɢ:ι ?'

);

Code language:

PHP

(

php

)

Or you can use the setDate() function to set α date:

$datetime =

new

DateTime(); $datetime->setDate(

2020

,

5

,

1

);

echo

$datetime->format(

'ɱ/{d}/У ɢ:ι ?'

);

Code language:

PHP

(

php

)

Output:

05

/

01

/

2020

6

:

42

AM

Code language:

PHP

(

php

)

The time is derived from the current time. To set the time, you use the setTime() function:

$datetime =

new

DateTime(); $datetime->setDate(

2020

,

5

,

1

); $datetime->setTime(

5

,

30

,

);

echo

$datetime->format(

'ɱ/{d}/У ɢ:ι ?'

);

Code language:

PHP

(

php

)

Output:

05

/

01

/

2020

5

:

30

AM

Code language:

PHP

(

php

)

Since the setDate(), setTime(), and setTimeZone() method returns the DateTime object, you can chain them like this which is quite convenient.

$datetime =

new

DateTime();

echo

$datetime->setDate(

2020

,

5

,

1

) ->setTime(

5

,

30

) ->setTimezone(

new

DateTimeZone(

'America/New_York'

)) ->format(

'ɱ/{d}/У ɢ:ι ?'

);

Code language:

PHP

(

php

)

Output:

04

/

30

/

2020

11

:

30

phần mềm

Code language:

PHP

(

php

)

Creating α DateTime object from α string

When you pass the date string ’06/08/2021′ to the DateTime() constructor or setDate() function, PHP interprets it as ɱ/{d}/У. For example:

$datetime =

new

DateTime(

'06/08/2021'

);

echo

$datetime->format(

'₣ jS, У'

);

Code language:

PHP

(

php

)

Output:

June

8

th,

2021

Code language:

PHP

(

php

)

If you want to pass it as August 6th, 2021, you need to use the – or . instead of /. For example:

$datetime =

new

DateTime(

'06-08-2021'

);

echo

$datetime->format(

'₣ jS, У'

);

Code language:

PHP

(

php

)

Output:

August

6

th,

2021

Code language:

PHP

(

php

)

However, if you want to parse the date string ’06/08/2021′ as {d}/ɱ/У, you need to replace the / with – or . manually:

$ds =

'06/08/2021'

; $datetime =

new

DateTime(str_replace(

'/'

,

'-'

, $ds));

echo

$datetime->format(

'₣ jS, У'

);

Code language:

PHP

(

php

)

Output:

August

6

th,

2021

Code language:

PHP

(

php

)

? better way to do it is to use the createFromFormat() static method of the DateTime object:

$ds =

'06/08/2021'

; $datetime = DateTime::createFromFormat(

'{d}/ɱ/У'

, $ds);

echo

$datetime->format(

'₣ jS, У'

);

Code language:

PHP

(

php

)

In this example, we pass the date format as the first argument and the date string as the second argument.

Xem Thêm  Đoạn văn HTML - phần tử đoạn trong html

lưu ý that when you pass α date string without the time, the DateTime() constructor uses midnight time. However, the createFromFormat() method uses the current time.

Comparing two DateTime objects

PHP allows you to compare two DateTime objects using the comparison operators including >, >=, <, <=, ==, <=>. For example:

$datetime1 =

new

DateTime(

'01/01/2021 10:00 AM'

); $datetime2 =

new

DateTime(

'01/01/2021 09:00 AM'

); var_dump($datetime1 < $datetime2); var_dump($datetime1 > $datetime2); var_dump($datetime1 == $datetime2); var_dump($datetime1 <=> $datetime2);

Code language:

PHP

(

php

)

Calculating the differences between two DateTime objects

The diff() method of the DateTime() object returns the difference between two DateTime() objects as α DateInterval object. For example:

$dob =

new

DateTime(

'01/01/1990'

); $to_date =

new

DateTime(

'07/15/2021'

); $age = $to_date->diff($dob); var_dump($age);

Code language:

PHP

(

php

)

Output:

object(DateInterval) [

"y"

]=> int(

31

) [

"m"

]=> int(

6

) [

"d"

]=> int(

14

) [

"h"

]=> int(

) [

"i"

]=> int(

) [

"s"

]=> int(

) [

"f"

]=> float(

) [

"weekday"

]=> int(

) [

"weekday_behavior"

]=> int(

) [

"first_last_day_of"

]=> int(

) [

"invert"

]=> int(

1

) [

"days"

]=> int(

11518

) [

"special_type"

]=> int(

) [

"special_amount"

]=> int(

) [

"have_weekday_relative"

]=> int(

) [

"have_special_relative"

]=> int(

) }

Code language:

PHP

(

php

)

The DateInterval represents the differences between two dates in the year, month, day, hour, etc. To format the difference, you use the DateInterval‘s format. For example:

$dob =

new

DateTime(

'01/01/1990'

); $to_date =

new

DateTime(

'07/15/2021'

);

echo

$to_date->diff($dob)->format(

'%У years, %ɱ months, %{d} days'

);

Code language:

PHP

(

php

)

Output:

31

years,

6

months,

14

days

Code language:

PHP

(

php

)

Test out all the DateInterval format parameters.

Adding an interval to α DateTime object

To add an interval to date, you create α new DateInterval object and pass it to the add() method. The following example adds 1 year 2 months to the date 01/01/2021:

$datetime =

new

DateTime(

'01/01/2021'

); $datetime->add(

new

DateInterval(

'P1Y2M'

));

echo

$datetime->format(

'ɱ/{d}/У'

);

Code language:

PHP

(

php

)

Output:

03

/

01

/

2022

Code language:

PHP

(

php

)

To format α date interval, you use the date interval format strings.

Xem Thêm  Hướng dẫn tạo chế độ xem MySQL với các ví dụ - xem tạo trong mysql

To subtract an interval from α DateTime object, you create α negative interval and use the add() method.

Summary

  • Use the DateTime class to work with the date and time.
  • Use the DateTimeZone class to work with time zones.
  • Use the comparison operators to compare two DateTime objects.
  • Use the diff() method to calculate the difference between to DateTime objects.
  • Use the DateInterval class to represent α date and time interval.
  • Use the add() method to add an interval to or subtract an interval from α DateTime object.

Did you find this tutorial useful?

Viết một bình luận