Wednesday, January 2, 2008

Internet - A Vital Need / PHP getdate() Function

It is vital to have an internet connection in your life. No matter what field you are in, internet makes your life a piece of cake when it comes to gathering information or learning.

I find myself clinging between two different fields, that is, IT and Creative Designing. Being a Graphics Designer at core - I don't find myself very strong in programming / scripting.

There was a task when I had to retrieve the date form the database which is OLDER THAN 1 MONTH and is left unverified (a specific system was designed when the user who filled the form will confirm his/her e-mail address by clicking on the link in the dynamically-generated e-mail in their inbox).

For that I need to find the current date and perform a date calculation to get the only data which is 1 month old. Simple additions and subtraction of the Months or Days will some day / month give a negative value and the system will crash. I did not know how to make proper date-calculation in mySQL queries or PHP, I was very confused how to achieve this task, the other way around was to write a conditional code with a lot of if and elseif to detect and solve this problem.

I spend more than an hour to figure out all the values that we get when we use a function getdate() -- Due to no internet connection! If there was an internet connection it would have taken a minute or two to find it out. Basically when you use the following code;



$cur_date = getdate() ;
print_r(
$cur_date) ;

?>


getdate()
function gets the current date and time of the system in an ARRAY. You will get an out put of this sort;

Array ( [seconds] => 10 [minutes] => 5 [hours] => 19 [mday] => 2 [wday] => 3 [mon] => 1 [year] => 2008 [yday] => 1 [weekday] => Wednesday [month] => January [0] => 1199282710 )
Notice the value in bold [0] => 1199282710 This is a very confusing value for me (as I didn't know the basics), anyway I figured out that it is number of seconds from the early / initial time.

-- I'm going to shorten the explanation here-for details you may contact / comment --

Windows Early / beginning time is 1st Jan 1970 05:00:00 (that is, 5 in the morning)
which in terms of timestamp is written as ZERO (0)

If you get a current timestamp it is the number of seconds added to the begining time, that is, zero.

So if you want to get to 1st of February 1970 then you should add something like this

(days * hours * minutes * seconds) this will give you the seconds in 1 month, which is;

31*24*60*60 = 2678400 = 1 Month = 31 Days
30*24*60*60 = 2592000 = 1 Month = 30 Days
24*60*60 = 86400 = 1 Day
3*24*60*60 = 259200 = 3 Days
7*24*60*60 = 604800 = 7 Days - Week

The system also knows which month has 28 days and 31 days. so when you are adding 2 months to a timestamp, note that its not always 2678400* 2 :)

Here is the code which I played with, take a look at it it may help you out.

$current_date = getdate();
$current_timestamp = $current_date['0'];
echo "Current Timestamp: " . $current_timestamp;
echo "
"
;

$current_date_formatted = date('d M, Y H:m:s A', $current_timestamp);
echo "Current Date Formatted: " . $current_date_formatted;
echo "
"
;

$current_date_sysformatted = date('Y-m-d H:m:s', $current_timestamp);
echo "Current Date System Formatted: " . $current_date_sysformatted;

echo "
"
;


$older_timestamp = ($current_timestamp - 2678400);
echo "Older Timestamp: " . $older_timestamp;
echo "
"
;

$older_date_formatted = date('d M, Y H:m:s A', $older_timestamp);
echo "Older Date Formatted: " . $older_date_formatted;
echo "
"
;

$older_date_sysformatted = date('Y-m-d H:m:s', $older_timestamp);
echo "Current Date System Formatted: " . $older_date_sysformatted;



Enjoy!