0

So, I have the following php:

<?php echo get_post_meta($post->ID,'dfd_ModificationTimestamp',true); ?>

It outputs dates as the following: 13/06/2015 11:02:18

I am not sure how to change it to "3 days ago" or "2 month ago" etc.

Any suggestions?

Thanks!

Steve Kim
  • 5,293
  • 16
  • 54
  • 99

3 Answers3

2

I think you are looking for the wordpress function human_time_diff

Example:

//Get your date
$date = get_post_meta($post->ID,'dfd_ModificationTimestamp',true);

//Convert it to a unix time stamp
$timestamp = strtotime($date);

//Print a nice string showing how long ago that was
echo human_time_diff( $timestamp, current_time('timestamp') ) . ' ago';
Mikk3lRo
  • 3,477
  • 17
  • 38
1

Use this:

$dateString = get_post_meta($post->ID,'dfd_ModificationTimestamp',true);
/* @var \DateTime $date */
$date = DateTime::createFromFormat('d/m/Y H:i:s', $dateString);

Now $date is DateTime object. You can modify it as you wish.

For example, you need 3 days ago from your date value(13/06/2015 11:02:18)

$date->modify('-3 days'); // $date becomes 10/06/2015 11:02:18

or if you need 2 months ago from your date value(13/06/2015 11:02:18)

$date->modify('-2 months'); // $date becomes 13/04/2015 11:02:18

To print $date value after modify, use:

echo $date->format('d/m/Y H:i:s');

See http://php.net/manual/en/class.datetime.php for more custom information.

sklwebdev
  • 281
  • 2
  • 9
1

I know you tagged this PHP but you can do this with javascript using the timeago plugin.

<abbr class="timeago" title="2008-07-17T09:24:17Z">July 17, 2008</abbr>

is converted into

<abbr class="timeago" title="July 17, 2008">7 years ago</abbr>

You need to format the time in ISO_8601 format too, something like this would do it:

echo (new DateTime('17 Oct 2008'))->format('c');

Sources:

http://timeago.yarp.com/

How to display a date as iso 8601 format with PHP

Community
  • 1
  • 1
rob_was_taken
  • 384
  • 4
  • 14