Fabrik Date Input – Get and Set the Week Number using PHP and JavaScript

12 Mar 2010

This piece of short and sweet code is quite useful when you need to get the week number of a certain date for special records, joins or calculations. I use it for one of my projects involving an Inventory Management System that is based only on weekends, so most of the orders and transactions need to be recorded and filtered by week numbers.

How to get the Week Number from a Date Input

1. Create an element – ‘Element Type: Date‘.

2. Under the ‘Table date format’ and ‘Form date format’ fields, key in your desired format and include %W anywhere in the format string. The %W will return the week number for the date that you choose. For me, I do it like this: %d-%m-%Y, Week %W which will return a date like so: 01-01-2010, Week 01.

3. If you want to play around with more PHP date functions, go to

Now that was very simple and I’m pretty sure most of you already know it. But how do you extract the Week number from that string and save it to a database table? Or use it for calculations? As a table filter maybe? Ok I’ll show you how.

How to Get the Week Number and Set it to an Input Field

Here’s the code:
var orderForm = oPackage.blocks.get('form_17').formElements;
var orderDate = orderForm.get('order_information___order_date').getValue();
var orderWeek = orderDate.split('k ');

Place this code in the JavaScript tab of your Date element and set the action: blur (onBlur).

So what in the world does this piece of code above do?
var orderForm = oPackage.blocks.get('form_17').formElements;[adsense1]
This is the standard variable assignment for getting the form elements and data. Replace ‘form_17’ with your form ID. View the source code of your web page to find your Form ID or get it from the Fabrik component Forms page in the Joomla! backend.

var orderDate = orderForm.get('order_information___order_date').getValue();
This one simply assigns the value of the date input field to ‘orderDate’. Change ‘order_information___order_date’ to the name of your date input field.

var orderWeek = orderDate.split('k ');
Split? Split what? Ok, this JavaScript split() method is used to split a string into an array of substrings. orderDate.split(‘k ‘) means that the orderDate string will be split into two at point ‘k ‘ and thrown into the orderWeek array. Ok you guess, what would orderWeek[0] return you? Yup, whatever string at and before ‘k ‘.

So this brings us to the final line that updates the value of another input field in your form to orderWeek[1], which is…… the Week number!

So there you have it – How to display a week number using PHP and then send its’ value to another field using JavaScript. Try it, it’s fun.

Author - Azhar Kamar

Write a Comment! NOW! :)

Current ye@r *