How to compare current date to article created date? Topic is solved

General questions relating to Joomla! 3.x.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting.
Forum Post Assistant - If you are serious about wanting help, you should use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10
Post Reply
klaes1
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Thu Mar 26, 2020 4:59 pm

How to compare current date to article created date?

Post by klaes1 » Thu Mar 26, 2020 5:06 pm

I need a code to compare if current date and time is greater than article created time of joomla. I use this inline script in html article but I get error: SyntaxError: Unexpected token '>'
Whats wrong with this code:

Code: Select all

<script>
// 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();
    });
');

</script>

 
User avatar
Per Yngve Berg
Joomla! Master
Joomla! Master
Posts: 26595
Joined: Mon Oct 27, 2008 9:27 pm
Location: Akershus, Norway

Re: How to compare current date to article created date?

Post by Per Yngve Berg » Thu Mar 26, 2020 5:34 pm

Are you writing JS or PHP? There seems to be a mix here.

klaes1
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Thu Mar 26, 2020 4:59 pm

Re: How to compare current date to article created date?

Post by klaes1 » Thu Mar 26, 2020 5:50 pm

Sir , I use this code in article content but its not working.

I need a code: when article created date and time is greater than current date and time than show a certain image. I will explain. I use article created date as the beginnig time of a soccer match which is in future in my article. So when article created date comes than show a image of 'spinning ball' which means soccer match had already started.

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 12049
Joined: Thu Feb 15, 2007 5:48 am
Location: Suzhou, China

Re: How to compare current date to article created date?

Post by toivo » Fri Mar 27, 2020 3:02 am

As @Per Yngve Berg pointed out, the javascript section contains PHP code, which generates a syntax error.

Create a content plugin where the dates are compared in PHP and then inject the javascript: https://docs.joomla.org/J3.x:Creating_a_content_plugin.

On the other hand, the article created date is used for a purpose it was not designed for. Use a custom field instead: https://docs.joomla.org/J3.x:Adding_custom_fields.
Toivo Talikka, Global Moderator

klaes1
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Thu Mar 26, 2020 4:59 pm

Re: How to compare current date to article created date?

Post by klaes1 » Fri Mar 27, 2020 8:11 am

Thank you for your time Sir but i'm not such expert to create plugin.

Any help to modify the code and make it only in javascript or in php or somehow separate the js and php?

User avatar
Per Yngve Berg
Joomla! Master
Joomla! Master
Posts: 26595
Joined: Mon Oct 27, 2008 9:27 pm
Location: Akershus, Norway

Re: How to compare current date to article created date?

Post by Per Yngve Berg » Fri Mar 27, 2020 8:29 am

Use php in s Layout Override.

Have you looked at JEvents which is a component designed to handle events?

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 12049
Joined: Thu Feb 15, 2007 5:48 am
Location: Suzhou, China

Re: How to compare current date to article created date?

Post by toivo » Fri Mar 27, 2020 8:42 am

If you do not want to develop a content plugin, set up a new Custom module and separate the javascript and PHP there, or develop your own module: https://docs.joomla.org/J3.x:Creating_a ... sic_Module.

However, PHP code can run from 'inside' an article through an extension that executes PHP code. Such extensions are available from the JED section Coding & Scripts Integration at https://extensions.joomla.org/category/ ... tegration/.

One popular integration extension is the recently updated system plugin Sourcerer, available from https://extensions.joomla.org/extension ... sourcerer/.
Toivo Talikka, Global Moderator

klaes1
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Thu Mar 26, 2020 4:59 pm

Re: How to compare current date to article created date?

Post by klaes1 » Sat Mar 28, 2020 7:00 am

Resolved! Thank you both for your help. After installing the system plugin Sourcerer to insert php code into an article it was quite easy with this php code:

Code: Select all

// PHP program to compare dates 
  
$date1 = new DateTime(); 
$date2 = new DateTime($article->created);
  
// Compare the dates 
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"); 

 

Post Reply

Return to “General Questions/New to Joomla! 3.x”