Override Wordpress core function _wp_mysql_week() - php

I would like to override the _wp_mysql_week() wordpress function located in wp-includes/functions.php since the actual _wp_mysql_week() is not complying with the ISO 8601 and asking the mode 1 to the WEEK() MYSQL function instead of the mode 3.
I'd like to put a filter or something in the theme functions.php but couldn't find how.
Thx for any help

The way this function is rigged up in WP 4.1, if you specify "Monday" as the first day of the week in the options table, you'll get ISO8601:1988 week numbers (MySQL Mode 1). Otherwise you'll get mode 0.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_week
You can also intervene with a filter after the query is constructed but before it's passed over to MySQL. In your filter you can muck around with the text of the query to change
WEEK( something, 0 )
to
WEEK( something, 1 )
to get your ISO 8601:1988 behavior. This explains query filtering. http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_clauses
Notice that the _wp_mysql_week function helpfully generates its output with spaces where I have shown pound signs here.
WEEK(#something,#1#)
If your week starts on some day besides Sunday or Monday, the generated query code looks like this:
WEEK(#DATE_SUB(#something,#INTERVAL#3#DAY#),#0#)
So, you can probably use a regex in your filter to search for
WEEK( .+? 0 )
then change the digit to 1. But of course regexs have hazards.
You could also request that a new filter be added specifically to the output of _wp_mysql_week in order to allow user code to control it. https://core.trac.wordpress.org/ The WordPress team is generally receptive to such requests.

Related

Extract an 8 character integer string from an ical file

This code to get all sequences of 8 integers works fine:
preg_match_all('/[0-9]{8}/', $string, $match);
However I am only interested if the match starts with 20.
I know I have to add ^20 somewhere but I have tried many times with no success. I have looked at many regex tutorials but none of them seems to explain how to do 2 separate searches.
I am actually trying to parse ICAL files to extract the dates. If the 8 digit integer starts with 20 it almost certainly is a date.
For example: DTSTART:20150112T120000Z
How about this solution:
/(20)\d{6}/
This will probably find what you are looking for:
(?=20)(\d{8})
It does a positive lookahead to capture a group if it starts with 20 along with a 8 digit number.
The answer highly depends on what you want to achieve. Do you want to extract all and any dates from an icalendar file. If so, you might be missing birthday dates as their year are most likely to be starting with 19xx.
Also matching any dates will yield most likely many undesired dates like UNTIL, TRIGGER, DTEND, ...
Assuming from your example you want to extract events start dates, you could try:
DTSTART[a-zA-Z._%+-/=;]*:(\d){8}[T]?[\d]{6}
To be kept in mind: following DTSTART can be a timezone definition like TZID=America/New_York and/or the type definition DATE or DATE-TIME (see RFC5545 DATE-TIME

Searchable date/time durations in SMW

I'm using Mediawiki, with the SMW extension, in a private setting to organize a fictional universe my group is creating. I have some functionality I'd like, and I'd like to know if there is an extension out there, or the possibility of making my own, that would let me do what I want to do.
Basically, in plain english... I want to know what's going on in the universe at a specific point (or duration) in time.
I'd like to give the user an ability to give a date (as simple as a year, or as precise as necessary), or duration, and return every event which has a duration that overlaps.
For example.
Event 1 starts ~ 3200 BCE, and ends ~ 198 BCE
Event 2 starts ~509 BCE and ends ~ 405 CE
Event 3 starts 1/15/419 CE and ends 1/17/419 CE
Event 4 starts ~409 BCE and ends on 2/14/2021 CE
User inputs a date (partial, in this instance) 309 BCE.
Wiki returns Event 1, and Event 4, as the given date is within the duration of both.
This would allow my creators to query a specific date (and hopefully a duration) and discover what events are already taking place, so they can adjust their works according to what is already established. It's a simple conflict checker.
If there's no extension available that can handle this, is there anything like this anywhere I can research? I've never dealt with dates in PHP. I'm a general business coder, I've never done complex applications.
There is no built in “duration” data type in SMW, so the easiest approach would probably be to use one date property for the starting date, and one for the ending date (note that it must be BC/AD, not BCE/CE or similar):
[[Event starts at::3200 BC]]
[[Event ends at::198 BC]]
then you can query for each event that has a starting date before, and an ending date after a certain date:
{{#ask:[[Event starts at::<1000 BC]] [[Event ends at::>1000 BC]]}}
Note that > actually means “greater or equal to” in SMW query syntax.

Building a text string from date details in MySQL

I've hunted around and found plenty of ways to convert a string to a date, but I can't seem to find one for the reverse... so here we go.
I'm trying to build a text string for a query, specifically running through WordPress on MySQL, for the BAW Post Count plugin data. That plugin uses a meta-key which always begins the same (count-views) and is followed by 'day', 'month' and so on, then the date part.
What I want to do is specify which meta-key I'm looking for by building a text string based on the current date:
"_count-views_month-" then the 4-figure year and two-figure month.
For example, this month is October 2013, so it would come out as "_count-views_month-201310"
The problem is that I can't seem to find a way to get the year and month as text, for use in a php script.
I've tried using year(now()),year(date(now())), cast, concat and a variety of other things but none seem to work. I know it must be incredibly easy and I'm just complicating matters or overlooking something obvious, but I can't see what!
So, the question is how I can do that as a "$variable=" statement, please.
Thanks!
You over-thought it:
echo date("Ym"); // outputs "201310"
Full example:
echo '_count-views_month-' . date("Ym");
Or:
printf("_count-views_month-%s", date("Ym"));
Or, in SQL:
SELECT CONCAT("_count-views_month-", DATE_FORMAT(CURRENT_DATE, "%Y%m"))

Show every 10th result Drupal 7 Views

I have Drupal 7 View. It contains 2249 results to be displayed on a graph. I am using the highcharts module to display that data. I have created a running total of the integer data with a global math expression field. I wanted to keep the expression's total on the graph but limit the amount of results as they are overlapping each other and it looks really bad.
I am using aggregation to combine days that are similar but it seems at this point date granularity is not available with the date module in views.
Is there a way to do this views php filter and use a bit of php to do the math so it would work ?
I have a Global:PHP filter that has this in it but it's not doing anything right maybe someone has a tip to get it working properly -
if( $row->id % 10 == 0 ) return TRUE;
thanks

Solr: calculating the difference between two given dates when one of the values is *

I'm working on something that when a range filter is active, the range facets will update accordingly. If no filter is given, I'm using a default range & gap. If a filter is set I do a second solr call to recalculate the facet range gap.
So for example at first I show range facets of a gap of 10YEARS. When you filter one of those, it shows 10 range facets of 1YEAR. If you filter again, it'll show 12 buckets with a 1 month range etc...
It kind of works, but I'm having trouble when the range filter includes a *. Solr knows how to correctly filter results when solrfield:[1960-01-01T00:00:00Z TO *] is given, but I don't know how I recalculate the date range facet gaps based on *. Right now I calculate the difference between the two dates as a unix timestamp and calculate the range gap based on that.
Also, is there a name for what I'm trying to do here? I'd call it 'variable range gaps', but I'm not sure if that's correct.
I'm not even sure if what I'm doing is the best approach. Any advice is welcome.
Edit: what my question comes down to is: I would like to calculate difference (as unix timestamp) between the highest and lowest date of a field in every solr query.
Edit 2: Every solr call I do takes about 300ms (on a relatively small index). That's quite a bit in case I do 2 extra calls with a date sort to find the highest and lowest date value in the query. Then performing the 4th solr call with the correct date gap (and from/to values), would get a bit slow.
You can probably turn it into familiar "limited date range" case. If it is impossible for your data to have datetime set in the future, your can replace * with NOW always receiving the same resultset (as no date can be later than current moment) and calculate gaps basing on current timestamp.

Categories