0
votes

Using Modx evo, I am trying to use the following snippet to display the date difference between published date and current date (in days), but getting weird output. What am I doing wrong?

<?php
$date2=$modx->documentObject['createdon'];
$date1=time();
$dateDiff = $date1 - $date2;
$daysOld = floor($dateDiff/(60*60*24));
return $daysOld;
?>
1
First what you need to do - check format of createdon: return print_r($modx->documentObject['createdon'],1); and then ask again.Vasis
It give me following output: 1360951562 (Looks like the unix date stamp :(.Santosh
how about $daysOld = round($dateDiff/(60*60*24)); ?Vasis
Okay used round as you suggested, yet the output is still incorrect. The createdon date is 2013/02/17 and it shows the output as 4 (should be 1 if current date is 18th).Santosh
Thanks Vasis, I should be using editedon parameter. And using round actually did work. It's working fine now. Thanks a ton.Santosh

1 Answers

0
votes

The thing is that the date is stored as a SQL-date, not a timestamp.

Read the docs: http://rtfm.modx.com/display/revolution20/Date+Formats

So, this should work:

$dateDiff = $time() - strtotime($modx->documentObject['createdon']);
$daysOld = floor($dateDiff/(60*60*24));
return $daysOld;