Saturday, August 11, 2012



Now, it is the Ramadan season, the month-long fasting for the Muslims. Everyday their start  and ending time for fasting (Sahri and Iftar, respectively) is not the same. It changes due to the change in sunrise and sunset time. It is something that is impossible or somewhat illogical to remember. So for the users' convenience I have created a code which will automatically give users the time of Sahri and Iftar for that day. And moreover, it does not require you to mumble with the MySQL databases or tables. It works based on a CSV file which we use to store the times for everyday.

Step 1: Create the data (but no database!)

Let's say, we are a little bit lazy for creating a database for this widget sort of thing. And besides, it is for one month only. So, we are going to store our time data in a .csv file. .csv files are easy to edit with any spreadsheet editor such as, Microsoft Office Excel, LibreOffice Calc, OpenOffice.org Calc etc. You are required to start your spreadsheet program then create a sheet like as follows (if you can't make this, download the source zip and get a copy of it):
Sahri Iftar time php widget csv storage
We have 6 columns: roza, day, month, year, sahri, iftar

roza: the serial number for the roza.
day, month, year: the date to which the roza is for.
sahri: time for Sahri.
iftar: time for Iftar.

Now that the csv file (or in our case the flat-file database) is ready, we can move on to the next part.


Step 2: Download CSV parser class

You can google for a php csv parser class, but mine seems to be working fine. So I am going to use "parseCSV" for getting our data from the csv file.
  1. Go to http://code.google.com/p/parsecsv-for-php/
  2. Go to Downloads link and download the class package.
  3. Extract the package and copy parsecsv.lib.php file to your working folder/directory

Step 3: Write code

We are using PHP for coding. So here is an example php file for simple usage.

<html>
<body>
<?php

require_once 'parsecsv.lib.php';

$csv = new parseCSV();
$csv->auto('timesdb.csv');

$search_day = date('j');
$search_month = date('n');
$data = $csv->data;

for ($i = 0; $i <= count($data) - 1; $i++) {
    if ($data[$i]['day'] == $search_day && $data[$i]['month'] == $search_month) {
        echo '<h3>Today ('.date('d-m-y').'):</h3>';
        echo '<p>Sahri: ' . $data[$i]['sahri'] . ', Iftar: ' . $data[$i]['iftar'].'</p>';
        echo '<h3>Tomorrow:</h3>';
        echo '<p>Sahri: ' . $data[$i+1]['sahri'] . ', Iftar: ' . $data[$i+1]['iftar'].'</p>';
    }
}


?>

</body>
</html>

Conclusion

Well, there you have it! A Sahri and Iftar widget for your website which requires no database. We use 3 files: (1) the CSV file, (2) CSV Parser, (3) PHP file. You may store the class file and the data file in separate directories for the sake  of clarity in your project's directory structure.

Here is the download link to the source files: http://www.divshare.com/download/19329139-21d

Feel free to comment in case you need any help or have a way to improve it.

Be Informed Whenever a New Post is Published.

If you enjoyed this article, SUBSCRIBE now for FREE to get regular updates delivered to your E-mail inbox.Your E-mail is safe with us - No spam, we promise.

0 comments:

Post a Comment

Follow Us

Blog Archive

Popular Posts