1

I need a code to compare if current date and time is greater than article created time of joomla. I have this example code:

  function CompareDate() {  
   //            new Date(Year, Month, Date, Hr, Min, Sec);  
   var dateOne = new Date(2020, 03, 20, 11, 00, 00);  
   var dateTwo = new Date(2020, 03, 20, 12, 00, 00);  

   if (dateOne > dateTwo) {  
        alert("Date One is greater than Date Two.");  
    }else {  
        alert("Date Two is greater than Date One.");  
    }  
}  
CompareDate();  

Where I need: 'dateOne' to be the current date and time and 'dateTwo' to be the article createad date and time

Any help with this?

New contributor
viper1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • Have you searched through our existing pages for datetime insights? We have already spoken thoroughly about server time, utc, and client-side timestamps. – mickmackusa Mar 26 at 23:09
  • Based on your answer, this is no longer a javascript question which makes your question Unclear. Also, here is a similar post: joomla.stackexchange.com/a/18810/12352 – mickmackusa Mar 28 at 21:01
  • yes Sir this was resolved via php. the post you point to... has nothing similar with my question . – viper1 Mar 29 at 9:44
  • While not an exact duplicate, it does demonstrate how to compare an article's date to a generated date. I will disagree that it "has nothing similar". Your exact scenario is only a more basic generated date. – mickmackusa Mar 29 at 9:55
  • the basic of my question was how to fetch an article created time of joomla article in php code, Sir. the other part of code was easy.Thank you – viper1 Mar 29 at 10:02
0

I'll assume you're using an inline script inside a PHP file. Depending on how you're fetching the creation date of the article, you may need to make some minor modifications:

// Get the creation date of the article
// This may need to be changed according to your requirements
$creationDate = $this->item->created;

Joomla\CMS\Factory::getDocument()->addScriptDeclaration('
    document.addEventListener("DOMContentLoaded", function() {
        function CompareDate()
        {
            var dateOne = new Date("' . $creationDate . '");
            var dateTwo = new Date(Date.now());

            if (dateOne > dateTwo)
            {  
                alert("Date One is greater than Date Two.");
            }
            else
            {  
                alert("Date Two is greater than Date One.");
            }  
        }

        CompareDate();
    });
');
|improve this answer|||||
  • yes I'm using inline script inside custom html article. I try your code but now I'm not getting any message which date is greater. – viper1 Mar 26 at 10:51
  • @viper - Try the updated code. Please report back any console errors. – Lodder Mar 26 at 10:56
  • I get this error: SyntaxError: Unexpected token '>' – viper1 Mar 26 at 11:01
  • You're going to need to updated your question with some more code then. Cause if I add this to: administrator\components\com_content\tmpl\article\edit.php it works perfectly fine. – Lodder Mar 26 at 11:54
  • this code should work but it contains both php and js code so how could it be separated – viper1 Mar 27 at 4:45
0

Resolved! This is a working php code to compare current date to article created date:

$date1 = new DateTime(); $date2 = new DateTime($article->created); if ($date1 > $date2) 
echo $date1->format("Y-m-d") . " is latest than "
        . $date2->format("Y-m-d"); else echo $date1->format("Y-m-d") . " is older than " 
        . $date2->format("Y-m-d"); 
|improve this answer|||||
New contributor
viper1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

viper1 is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.