- Posts: 25
COMMUNITY FORUM
- Forum
- K2 Community Forum
- English K2 Community
- Style css when an item from a category is written today
Style css when an item from a category is written today
- Slartibartfast
-
Topic Author
- Offline
- Junior Member
i have a side menu with items to different categories.
each category may or may not have an article/item written per day. not more than one though.
So, i wan to show a "new" -icon/gif whatever- next to the menu item of each category that has a new article written today.
i have managed to do it by:
- creating a custom template in my_theme/html/mod_menu
- using the publish_up field of the jos_k2_items table.
but because my timezone is +3 hours to UTC the article must be written after 3am ...
my server time is UTC. this cannot be changed
my joomla timezone is set to Europe/Athens
K2 stores UTC time in publish_up field
here is my query, but i start to believe that i need a completely different approach
$nowdate = JFactory::getDate('now', new DateTimeZone('Europe/Athens'));
$curdate = JFactory::getDate('now', new DateTimeZone('Europe/Athens'))->format('Y-m-d', TRUE);
$query->select("distinct ". $db->quoteName('catid'));
$query->from($db->quoteName('*****_k2_items'));
$query->where($db->quoteName('publish_up') .' <= '. $db->quote( $nowdate ));
$query->where('Date('.$db->quoteName('publish_up').')' . ' = '. $db->quote( $curdate ));
$query->where($db->quoteName('published') .' = 1');
$query->where($db->quoteName('trash') .' = 0');
$query->order('id ASC');
any ideas would be much appreciated :)
Please Log in or Create an account to join the conversation.
- JoomlaWorks
-
- Offline
- Admin
- Posts: 6227
You can just use the item creation/modified date and compare it with the current time. I would go even further and use JS to compare the time with the user's actual timezone.
I'm guessing you probably know your way around PHP and JS, so just grab the $item->created or $item->modified date (UTC) and print it as a data-attribute (e.g. data-created="2018-08-31") in the item's title or container. Then use JS to read all the data attributes, compare them to the user's actual time and if the difference is less than say 24 hrs, apply a new class dynamically which will show a <span> element that contains the word "New" (or some image).
Please Log in or Create an account to join the conversation.
- Slartibartfast
-
Topic Author
- Offline
- Junior Member
- Posts: 25
well unfortunately i am more of a "other people's code editor". So i am not sure i follow your thinking. i have 8 menu items, which equals 8 categories equals 8 possible "spans"
so am i suppose to code the mod_menu as before?
and second, how am i going to grab the data attributes of the item's container when i am in the home page?
Obviously i lost you there
it would be great help for me if you could specify your thinking a little bit more. then i could search my way out of this :)
Please Log in or Create an account to join the conversation.
- JoomlaWorks
-
- Offline
- Admin
- Posts: 6227
Try removing ", new DateTimeZone('Europe/Athens')" from your $now & $cur variables. Change the timezone in Joomla's global config to match your timezone. And you should be fine in regards to timezone differences, because these 2 variables always return the server's time values, not the user's (from their browser). That should work.
Please Log in or Create an account to join the conversation.
- Slartibartfast
-
Topic Author
- Offline
- Junior Member
- Posts: 25
- changed to JFactory::getDate('now');
- in global configuration the timezone is already "Athens"
still echo $curdate; and echo $nowdate; are returning UTC while JFactory::getDate('now', new DateTimeZone('Europe/Athens')); returns the correct time
i think that the problem is at the field "jos_k2_items". if i write an article at 2am on September 2nd, the field stores 23:00 01/09/2018 cause it stores UTC.
so when this is compared with the current date (02/09/2018) it obviously does not show the <span> since it "thinks" that the article was written yesterday.
Please Log in or Create an account to join the conversation.
- JoomlaWorks
-
- Offline
- Admin
- Posts: 6227
The problem is clearly in Joomla or some server configuration that you can't get the offset. But you can change that if you want manually, see here: docs.joomla.org/How_to_use_JDate
Or change the offset like this: joomla.stackexchange.com/a/4762/7022
You have 2 ways. Keep in mind that for Greece you need to adjust for DST (daylight saving time) as Joomla's offset will return +2 when it's currently +3.
But fear not! Our troubles may soon come to an end: www.theguardian.com/world/2018/aug/31/eu-recommend-member-states-abolish-daylight-saving-time
Please Log in or Create an account to join the conversation.
- Slartibartfast
-
Topic Author
- Offline
- Junior Member
- Posts: 25
i meant that as a problem for my code not a k2 problem ;)
But still, after you last post, if i understand correctly, you suggest that the date saved in jos_k2_items publish_up field should be saved in the timezone set in the global configuration? in my case Athens.
If that is the case then why my joomla/k2 installation saves in UTC?
and again just to clarify:
i have an article with publish_up date 2018-09-03 03:03 (Athens)
in the database the field is saved in UTC 2018-09-00 03:03
as for the daylight-saving ... if you like John Oliver check this
Please Log in or Create an account to join the conversation.
- JoomlaWorks
-
- Offline
- Admin
- Posts: 6227
Joomla does the job of converting UTC times to local times when content is displayed and any component is using the Joomla date functions to parse dates from the DB (like K2 does).
Please Log in or Create an account to join the conversation.
- Slartibartfast
-
Topic Author
- Offline
- Junior Member
- Posts: 25
how can i convert "publish_up" from UTC to my timezone(EEST ot EET) so the below query does not compare different timezones like it does now (UTC <= EEST):
$query->where($db->quoteName('publish_up') .' <= '. $db->quote( $nowdate ));
Please Log in or Create an account to join the conversation.
- JoomlaWorks
-
- Offline
- Admin
- Posts: 6227
Please Log in or Create an account to join the conversation.
- Slartibartfast
-
Topic Author
- Offline
- Junior Member
- Posts: 25
Please Log in or Create an account to join the conversation.
- JoomlaWorks
-
- Offline
- Admin
- Posts: 6227
Please Log in or Create an account to join the conversation.
- Slartibartfast
-
Topic Author
- Offline
- Junior Member
- Posts: 25
Specs: Centos 7.5, MariaDB 5.5, plesk 17.8
we need to load the timezones to mysql. (used this excellent post*)
i run
SELECT CONVERT_TZ('2012-06-07 12:00:00', 'GMT', 'America/New_York');
so i logged with ssh and give
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u admin -p`cat /etc/psa/.psa.shadow` mysql
remember this is a plesk installation. use "-u root" and your mysql password if not plesk.
you may get 2 warnings
Warning: Unable to load '/usr/share/zoneinfo/leapseconds' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/tzdata.zi' as time zone. Skipping it.
based on this* you are good to go.
now zones are loaded. you can run the previous query to test it.
and now we can use mick's query to convert the timezone properly.
$query->where("DATE(CONVERT_TZ(publish_up, 'GMT', 'Europe/Athens')) = DATE(CONVERT_TZ(NOW(), 'GMT', 'Europe/Athens'))");
@Fotis thank you for your help! using JDate, i think i tried it in the past. i couldn't come with any solution. if you have any suggestions i could happily try them.
*unfortunately links are not accepted by the editor so i had to delete them
Please Log in or Create an account to join the conversation.
- JoomlaWorks
-
- Offline
- Admin
- Posts: 6227
Using JDate, I would leave all dates as UTC, compare them as UTC and simply offset the results based on JDate to the user.
Please Log in or Create an account to join the conversation.
- Forum
- K2 Community Forum
- English K2 Community
- Style css when an item from a category is written today