Một năm trước, mình phát triển một plugin liên quan đến thời gian biểu. Và mình sớm nhận ra rằng có nhiều phương thức về thời gian trong PHP. Kết hợp với các hàm WordPress có sẵn, mình đã bị ngợp.
Vì vậy, hôm nay mình sẽ chỉ cho bạn những cách tốt nhất để làm việc với đối tượng date time trong WordPress.
Mục lục
- 1 Định dạng DateTime Định dạng DateTime
- 2 Đối tượng DateTime Đối tượng DateTime
- 2.1 Ngày hiện tại Ngày hiện tại
- 2.2 Theo ID bài viết Theo ID bài viết
- 2.3 Chuyển đổi từ một chuỗi [string] Chuyển đổi từ một chuỗi [string]
- 3 Khoảng cách giữa các DateTime Khoảng cách giữa các DateTime
- 4 Sửa đổi DateTime Sửa đổi DateTime
- 5 Lời kết Lời kết
Định dạng DateTime
Bảng dưới đây là định dạng DateTime phổ biến. Danh sách đầy đủ các bạn có thể tham khảo trong tài liệu của PHP.
Year::
Y
: 2022y
: 22
Month::
M
: Jan – Decm
: 01 – 12$post_date = get_post_datetime[ $post_id ]; echo $post_date->format[ 'd/m/Y' ];
0: January – December
Day::
$post_date = get_post_datetime[ $post_id ]; echo $post_date->format[ 'd/m/Y' ];
1: Mon – Sun$post_date = get_post_datetime[ $post_id ]; echo $post_date->format[ 'd/m/Y' ];
2: 01 – 31
Hour::
$post_date = get_post_datetime[ $post_id ]; echo $post_date->format[ 'd/m/Y' ];
3: 00 – 23$post_date = get_post_datetime[ $post_id ]; echo $post_date->format[ 'd/m/Y' ];
4: 01 – 12$post_date = get_post_datetime[ $post_id ]; echo $post_date->format[ 'd/m/Y' ];
5: AM hoặc PM
Minute::
$post_date = get_post_datetime[ $post_id ]; echo $post_date->format[ 'd/m/Y' ];
6: 01 – 59
Second::
$post_date = get_post_datetime[ $post_id ]; echo $post_date->format[ 'd/m/Y' ];
7: 01 – 59
Đối tượng DateTime
Ngày hiện tại
$current_date = current_datetime[]; echo $current_date->format[ 'd/m/Y' ]; // nếu bạn muốn sử dụng định dạng theo cài đặt echo $current_date->format[ get_option['date_format'] ];
Theo ID bài viết
$post_date = get_post_datetime[ $post_id ]; echo $post_date->format[ 'd/m/Y' ];
Chuyển đổi từ một chuỗi [string]
$meta_date = '13/01/2020'; $datetime = DateTime::createFromFormat[ 'd/m/Y', $meta_date ]; echo $datetime->format[ 'd M Y' ]; // 13 Jan 2020
Ngày xuất bản của Bài viết và Bình luận được lưu trữ với định dạng
$post_date = get_post_datetime[ $post_id ]; echo $post_date->format[ 'd/m/Y' ];8. Nếu chuỗi ở định dạng đó, bạn có thể sử dụng đoạn code dưới đây:
$post_date = '2020-01-16 12:00:00'; $datetime = new DateTime[ $post_date ]; echo $datetime->format[ 'd M Y' ]; // 16 Jan 2020
Khoảng cách giữa các DateTime
Nếu bạn muốn biết khoảng cách giữa 2 DateTime theo tháng, ngày, giờ thì bạn có thể tham khảo đoạn code dưới đây:
$datetime1 = DateTime::createFromFormat[ 'd/m/Y H:i', '10/02/2019 10:00' ]; $datetime2 = DateTime::createFromFormat[ 'd/m/Y H:i', '10/02/2020 23:00' ]; $diff = $datetime1->diff[ $datetime2 ]; $days_ago = $diff->days; $months_ago = $diff->m + [$diff->y * 12]; $hours_ago = $diff->h + [$diff->days * 24]; echo "{$days_ago} days ago"; // 365 ngày trước echo "{$hours_ago} hours ago"; // 8776 giờ trước echo "{$months_ago} months ago"; // 12 tháng trước
Sửa đổi DateTime
Trong trường hợp bạn muốn thay đổi DateTime. Có thể là tăng thêm hoặc giảm đi bao nhiêu ngày, hoặc tháng cụ thể. Thì bạn có thể dùng đoạn code dưới đây:
$datetime = DateTime::createFromFormat[ 'd/m/Y', '10/02/2020' ]; $datetime->modify[ '+1 day' ]; echo $datetime->format[ 'd M Y' ]; // 11 Feb 2020 $datetime->modify[ '+2 day +1 month' ]; echo $datetime->format[ 'd M Y' ]; // 14 Mar 2020 $datetime->modify[ '-10 day -2 month -1 year' ]; echo $datetime->format[ 'd M Y' ] ]; // 04 Jan 2019
Lời kết
Mình hy vọng bài viết này sẽ giúp được các bạn. Đặc biệt là với những bạn đang làm việc với theme hoặc plugin liên quan đến thời gian trong WordPress.
Nếu bài viết này hữu ích và giúp tiết kiệm được thời gian của bạn, hãy giúp mình chia sẻ nó. Ngoài ra nếu bạn quan tâm đến các chủ đề tương tự như vậy, hãy đọc các bài viết Thủ thuật WordPress khác và follow Fanpage để không bỏ lỡ bài viết mới từ mình nhé.
4.84votes 4 votes
Đánh giá bài viết
03/11/2018 08:53 26697 26697
Nội dung bài viết
- Lưu ý về function strtotime
- Xử lý date time trong wordpress [Handling date time in wordpress]
- Vòng lặp from date to date trong php
- Tính toán date từ ngày hiện tại
Lưu ý về function strtotime
function strtotime chỉ nhận “19-10-2022 15:57” nếu “19/10/2022 15:57” nó sẽ trả về kết quả sai nhé
Xử lý date time trong wordpress [Handling date time in wordpress]
timezone_identifiers_list[]: lấy tất cả timezone string
date_default_timezone_get[]:xem thử hiện tại server đang sử dụng default timezone gì
date_default_timezone_set[‘America/Los_Angeles’]: cấn set default time zone ở đầu file để việc xử lý date time được chính xác
date_default_timezone_set['Asia/Ho_Chi_Minh'];['Asia/Ho_Chi_Minh']; |
strtotime[ ‘2018-11-04 15:03:03’]: chuyển dử liệu date time trong mysql về dạng timestamp
date[‘Y-m-d H:i:s’,’1541372583′]: chuyển từ timestamp về date time trong mysql
time[]:lấy timestamp hiện tại
Vòng lặp from date to date trong php
$date='2015-01-01';//start datedate='2015-01-01';//start date $end_date='2015-01-31';//end dateend_date='2015-01-31';//end date $dates=array[];dates=array[]; while[strtotime[$date] [strtotime[$date]2015-01-01[0]=>2015-01-01 [1]=>2015-01-02[1]=>2015-01-02 [2]=>2015-01-03[2] =>2015-01-03 [3]=>2015-01-04[3]=>2015-01-04 [4]=>2015-01-05[4]=>2015-01-05 [5]=>2015-01-06[5] =>2015-01-06 [6]=>2015-01-07[6]=>2015-01-07 [7]=>2015-01-08[7]=>2015-01-08 [8]=>2015-01-09[8] =>2015-01-09 [9]=>2015-01-10[9]=>2015-01-10 [10]=>2015-01-11[10]=>2015-01-11 [11]=>2015-01-12[11] =>2015-01-12 [12]=>2015-01-13[12]=>2015-01-13 [13]=>2015-01-14[13]=>2015-01-14 [14]=>2015-01-15[14] =>2015-01-15 [15]=>2015-01-16[15]=>2015-01-16 [16]=>2015-01-17[16]=>2015-01-17 [17]=>2015-01-18[17] =>2015-01-18 [18]=>2015-01-19[18]=>2015-01-19 [19]=>2015-01-20[19]=>2015-01-20 [20]=>2015-01-21[20] =>2015-01-21 [21]=>2015-01-22[21]=>2015-01-22 [22]=>2015-01-23[22]=>2015-01-23 [23]=>2015-01-24[23] =>2015-01-24 [24]=>2015-01-25[24]=>2015-01-25 [25]=>2015-01-26[25]=>2015-01-26 [26]=>2015-01-27[26] =>2015-01-27 [27]=>2015-01-28[27]=>2015-01-28 [28]=>2015-01-29[28]=>2015-01-29 [29]=>2015-01-30[29] =>2015-01-30 [30]=>2015-01-31[30]=>2015-01-31 ] |
Tính toán date từ ngày hiện tại
date_default_timezone_set['Asia/Ho_Chi_Minh'];['Asia/Ho_Chi_Minh']; $after_day_number=$possible_delivery_time;after_day_number=$possible_delivery_time; $after_date=time[]+3*24*60*60;after_date=time[]+3*24*60*60; returndate["d/m/Y H:i",$after_date]; date["d/m/Y H:i",$after_date]; |
có thể bạn cần xem:
- Disable animation trong revolution slider [ 20/09/2022 08:07 ][ 20/09/2022 08:07 ]
- Cách tạo button printer một element bằng javascript [ 19/09/2022 12:56 ][ 19/09/2022 12:56 ]
- Code sử dụng vue js, quasar, axios, sweetalert2 trong tác vụ quản lý danh sách [ 19/07/2022 10:26 ][ 19/07/2022 10:26 ]
- Hướng dẫn lấy botToken và chatId của bot telegram [ 13/06/2022 13:54 ][ 13/06/2022 13:54 ]
- Tạo thanh bar bottom mobile trong theme wordpress [ 07/06/2022 10:00 ][ 07/06/2022 10:00 ]
- Fix acf not save iframe [ 23/04/2022 07:44 ][ 23/04/2022 07:44 ]
- Rewrite lại url cho page template wordpress [ 21/04/2022 02:11 ][ 21/04/2022 02:11 ]
- Tối ưu hóa cron trong wordpress để website chạy nhanh hơn [ 09/04/2022 03:30 ][ 09/04/2022 03:30 ]
- Function tạo rating star sử dụng dashicon từ wordpress [ 03/04/2022 03:02 ][ 03/04/2022 03:02 ]
- Thêm editor cho mô tả category wordpress [add editor description for category wordpress] [ 01/04/2022 02:02 ][ 01/04/2022 02:02 ]