Thursday, June 13, 2013

Year 2038 problem in 32 bit machine

The unix time stamp is a way to track time as a running total of seconds. This count starts at the Unix Epoch on January 1st, 1970. Therefore, the unix time stamp is merely the number of seconds between a particular date and the Unix Epoch.

On January 19, 2038 the Unix Time Stamp will cease to work due to a 32-bit overflow. The last timestamp that is working in 32 bit is 19.01.2038 03:14:07 ( =2147480047 ) After this second, timestamp number crosses 32 bit signed integer range.

Before this moment millions of applications will need to either adopt a new convention for time stamps or be migrated to 64-bit systems which will buy the time stamp a "bit" more time.

In PHP most important date functions are:
date and strtotime
We will use both functions in simple calendar application.


date: Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given.
Syntax: string date ( string $format [, int $timestamp = time() ] )


strtotime: Parse about any English textual datetime description into a Unix timestamp
Syntax: int strtotime ( string $time [, int $now = time() ] )

We should relay on unix timestamp in above two functions.

As per 32 bit Signed integer storage capacity, the unix timestamp will not parse by date function after the above mentioned date.

So, we need an alternate for this issue:


In astronomical calculations, we use Julian Day for day-number calendar. The Julian Day Number (JDN) is the integer assigned to a whole solar day in the Julian day count starting from noon Greenwich Mean Time, with Julian day number 0 assigned to the day starting at noon on January 1, 4713 BC

The Julian Date of any instant is the Julian day number for the preceding noon plus the fraction of the day since that instant. Julian Dates are expressed as a Julian day number with a decimal fraction added. The Julian Date for 22:44, 17 May 2013 (UTC) is 2456430.4476852.


Solution:

For the dates after January 19, 2038 we can make use of this Julian day-number and we can convert this Julian day number to Western/Gregorian calendar month day and year format.

For that we have two built in PHP functions:
gregoriantojd and JDToGregorian

These two functions are not relying on Unix TimeStamp. A simple arithmetic operations can do these conversions.


Below are the user defined functions which can give the idea on this date conversion algorithm.


// calculate Julian Day from Month, Day and Year
function mdy2julian($m,$d,$y)
{
$im = 12 * ($y + 4800) + $m - 3;
$j = (2 * ($im - floor($im/12) * 12) + 7 + 365 * $im)/12;
$j = floor($j) + $d + floor($im/48) - 32083;
if($j > 2299171) $j += floor($im/4800) - floor($im/1200) + 38;
return $j;
}


//calculate gregorian day to the julian day-number
function jd_to_greg($julian) {
$julian = $julian - 1721119;
$calc1 = 4 * $julian - 1;
$year = floor($calc1 / 146097);
$julian = floor($calc1 - 146097 * $year);
$day = floor($julian / 4);
$calc2 = 4 * $day + 3;
$julian = floor($calc2 / 1461);
$day = $calc2 - 1461 * $julian;
$day = floor(($day + 4) / 4);
$calc3 = 5 * $day - 3;
$month = floor($calc3 / 153);
$day = $calc3 - 153 * $month;
$day = floor(($day + 5) / 5);
$year = 100 * $year + $julian;

if ($month < 10) {
$month = $month + 3;
}
else {
$month = $month - 9;
$year = $year + 1;
}
return "$day/$month/$year";
}


References:
https://bugs.php.net/bug.php?id=7103
http://en.wikipedia.org/wiki/Year_2038_problem
http://en.wikipedia.org/wiki/Julian_day
http://php.net/manual/en/function.gregoriantojd.php
http://www.php.net/manual/en/function.jdtogregorian.php

Sunday, November 20, 2011

PHP mysql_connect Vs mysql_pconnect

  • Both "mysql_connect" and "mysql_pconnect" are used to connect MySQL database by PHP. Both are having similar function signature.
  • Main difference is, if we use "mysql_pconnect", this function would first try to find a persistant link that is already open with the same host, username and password.
  • In other words, mysql_pconnect() will try to find a connection that's already open, with the same host, username and password. If one is found, this will be returned instead of opening a new connection
  • "mysql_close" function can't close the database object if we connect the database with "mysql_pconnect" method.
  • For low traffic websites, its better to use mysql_pconnect method to increase the performance of the website. And for high traffic websites we should use "mysql_connect". Because, in high traffic, lot of database connections will be opened and they will lag the system performance.

Tuesday, May 17, 2011

Cookies in PHP

There are two types of Cookies present in PHP. One is Persistant Cookie and the other one is Temporary Cookie.
By default, cookies are created as temporary cookies which stored only in the browser's memory. A persistent cookie is a cookie which is stored in a cookie file permanently on the browser's computer. When the browser is closed, temporary cookies will be erased. You should decide when to use temporary cookies and when to use persistent cookies based on their differences:
  • Temporary cookies can not be used for tracking long-term information.
  • Persistent cookies can be used for tracking long-term information.
  • Temporary cookies are safer because no programs other than the browser can access them.
  • Persistent cookies are less secure because users can open cookie files see the cookie values

What is MVC Architecture

Model-view-controller (MVC) is a design pattern used in software engineering. In complex computer applications that present lots of data to the user, one often wishes to separate data (model) and user interface (view) concerns, so that changes to the user interface do not impact the data handling, and that the data can be reorganized without changing the user interface. The model-view-controller design pattern solves this problem by decoupling data access and business logic from data presentation and user interaction, by introducing an intermediate component: the controller.

Monday, May 2, 2011

File Uploading with out using Ajax

We can't upload file using normal Ajax. For that we should use advanced javascript frameworks or JSON for passing file content. Also this javascript will not work if it is disabled from the browser.
But we can get this Ajax effect without using ajax or javascript by simply depending on target attribute of the form tag.
Below is the sample script to achive this.



File name: form.html
<iframe src="http://www.genengnews.com/app_themes/genconnect/images/default_profile.jpg" id="targetFrame" name="targetFrame" style="border: 0; height:230px; width: 230px;"></iframe>
<form action="upload.php" target="targetFrame" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="upload" />
</form>


File name: upload.php
<?php
if(isset($_FILES["file"])) {
 // You can add file uploading script here
 echo "Temporary file location is: " . $_FILES["file"]["tmp_name"] . "<br />";
 if(substr($_FILES["file"]["type"], 0, 5) == "image") {
  echo '<img src="' . $_FILES["file"]["tmp_name"] . '" alt="" />';
 }
?>
<?php
}
?>

Wednesday, April 20, 2011

PHP Access Specifiers

Access Specifiers are the keywords in PHP, which will tell us about the visiblity of property or method.
Public, Private and Protected are the three Access Specifier Keywords in PHP. Below is the difference between each of them.
Class members declared public can be accessed everywhere.
Members declared protected can be accessed only within the class itself and by inherited and parent classes.
Members declared as private may only be accessed by the class that defines the member.
Default access specifier is Public.

Monday, April 18, 2011

HTML Optimization Techniques

Below are some of the techniques for HTML optimization:
1) Minimal Meta Tags usage: Minimizing the size of page's HEAD is an effective means to achieve quicker parsing of the HEAD, followed by the rest of the content of your website, in equally quick succession. The conditional SSI META tags with 200 characters or less and HTML lines with maximum 255 characters fetch favorable search engine results by saving space for browsers visiting your site. Further, there's no need to use commas to separate keywords, as search engines ignore the commas.
2) CSS Layout: Inline CSS will slow down the process page loading and page rendering. Always it will be better to link an external css file to the HTML file. Don't use import statement for css loading.
3) Javascript: Optimization of JavaScript is achieved with short variable names and no spaces etc, which speeds up the display of Web Pages. Avoid writing javascript in HTML file. Always include this javascript file at the end of the page to improve page loading speed.
4) Use DIVs rather than Tabes: Tables will load very slowly compare to DIV. We can achieve complete table layout by using Divs with simple CSS.
5) URL Abbreviation: By abbreviating URL using Apache's mod_rewrite module, we achieve effective cut down to your HTML file size.
6) HTML Compression: Compressing your HTML using utility like mod_gzip, automatically cuts down size of the web page for as much as 40% bandwidth and thus speeds up the page display immensely.
7) Reduce Images: Try to use low size image files in the layout. And try to reduce image backgrounds and achieve it by css properties.
8) Use Sprites: By using Sprites we can reduce maximum number of image requests on server. We can simply use one image for several image displays by modifying its position by using CSS.
9) Use multiple servers for external files: Try to store images and external javascript and css files in sub domains and try to use them in the webpage. By this way both html an images will be loaded simultaneously.