Working with DateTime in WordPress – Learn WordPress from a to z

Tutorials 0 lượt xem

A year ago, I developed a plugin related to the timetable. And I soon realized that there are many time methods in PHP. Combined with the available WordPress functions, I was overwhelmed.

Working with DateTime

So, today I will show you the best ways to work with date time object in WordPress.

DateTime format

The table below is the common DateTime format. A full list can be found in the PHP documentation .

Year :

  • Y : 2022
  • y : 22

Month :

  • M : Jan – Dec
  • m : 01 – 12
  • F : January – December

Day :

  • D : Mon – Sun
  • d : 01 – 31

Hours :

  • H : 00 – 23
  • h : 01 – 12
  • A : AM or PM

Minute :

  • i : 01 – 59

Second :

  • s : 01 – 59

DateTime object

Today

$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') );
<div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span>
</div>

By post ID

$post_date = get_post_datetime( $post_id );
echo $post_date-&gt;format( 'd/m/Y' );
<div>
<span>1</span><span>2</span>
</div>

Convert from a string (string)

$meta_date = '13/01/2020';
$datetime = DateTime::createFromFormat( 'd/m/Y', $meta_date );

echo $datetime-&gt;format( 'd M Y' ); // 13 Jan 2020
<div>
<span>1</span><span>2</span><span>3</span><span>4</span>
</div>

The publication date of Posts and Comments is stored with the format Y-m-d H:i:s . If the string is in that format, you can use the code below:

$post_date = '2020-01-16 12:00:00';
$datetime = new DateTime( $post_date );

echo $datetime-&gt;format( 'd M Y' ); // 16 Jan 2020
<div>
<span>1</span><span>2</span><span>3</span><span>4</span>
</div>

Spacing between DateTimes

If you want to know the distance between 2 DateTime by month, day, and time, you can refer to the code below:

$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-&gt;diff( $datetime2 );
$days_ago = $diff-&gt;days;
$months_ago = $diff-&gt;m + ($diff-&gt;y * 12);
$hours_ago = $diff-&gt;h + ($diff-&gt;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
<div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span>
</div>

Modify DateTime

In case you want to change DateTime. It can be increased or decreased by how many days, or specific months. Then you can use the following code:

$datetime = DateTime::createFromFormat( 'd/m/Y', '10/02/2020' );

$datetime-&gt;modify( '+1 day' );
echo $datetime-&gt;format( 'd M Y' ); // 11 Feb 2020

$datetime-&gt;modify( '+2 day +1 month' );
echo $datetime-&gt;format( 'd M Y' ); // 14 Mar 2020

$datetime-&gt;modify( '-10 day -2 month -1 year' );
echo $datetime-&gt;format( 'd M Y' ) ); // 04 Jan 2019
<div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span>
</div>

Epilogue

I hope this article will help you. Especially for those of you who are working with time-related themes or plugins in WordPress.

If this article was helpful and saved your time, please help me share it. Also if you are interested in similar topics, read other WordPress Tips articles and follow Fanpage so you don’t miss new posts from me.

Bài viết liên quan