Often we need to subtract years, months, days, hours, minutes and even seconds from current time or any other time. Previously I wrote a script and/or tutorial on how to add time. This script is nothing but a little modified version of the previous script to deduct or subtract time. I wrote this script since several people often come to search the time adding script. So, I wrote this script to check if this is helpful for any one.
Okay. Here is the PHP function to subtract or decuct time.
<?php
function subtractTime($hours=0, $minutes=0, $seconds=0, $months=0, $days=0, $years=0)
{
$totalHours = date(“H”) – $hours;
$totalMinutes = date(“i”) – $minutes;
$totalSeconds = date(“s”) – $seconds;
$totalMonths = date(“m”) – $months;
$totalDays = date(“d”) – $days;
$totalYears = date(“Y”) – $years;
$timeStamp = mktime($totalHours, $totalMinutes, $totalSeconds, $totalMonths, $totalDays, $totalYears);
$myTime = date(“Y-m-d H:i:s A”, $timeStamp);
return $myTime;
}
?>
Now we call the function to test the output.
<?php
// Let us first see the current time
echo ‘Current Time: ‘ . date(“Y-m-d H:i:s A”);
echo ‘<BR>’;
// Now let us deduct 5 hours, 2 days and 1 year from now
echo ‘New Time: ‘ . subtractTime(5,0,0,0,2,1);
?>
And here is a sample output:
Current Time: 2008-03-23 21:40:28 PM
New Time: 2007-03-21 16:40:28 PM
Thus, you can subtract or deduct time (such as second, minute, hour, day, month, year) easily with a simple function.
PHP script to add time. Tutorial with code and example
February 26, 2008
Some days ago I wrote an article on how to get the local time. But I saw that many people are searching with keywords like “PHP Add Time”. As this is happening frequently, I have decided to write an article on how to add time using PHP. Here is the article. In this article, I have written a function that takes hour, minute, second, day, month and year as inputs, adds all supplied time values and returns the new time.
So, you can add hours, minutes, seconds, days, months and years with current time by supplying only 6 inputs when you call the function.
Okay. Here is the PHP function to add time.
<?php
function addTime($hours=0, $minutes=0, $seconds=0, $months=0, $days=0, $years=0)
{
$totalHours = date(“H”) + $hours;
$totalMinutes = date(“i”) + $minutes;
$totalSeconds = date(“s”) + $seconds;
$totalMonths = date(“m”) + $months;
$totalDays = date(“d”) + $days;
$totalYears = date(“Y”) + $years;
$timeStamp = mktime($totalHours, $totalMinutes, $totalSeconds, $totalMonths, $totalDays, $totalYears);
$myTime = date(“Y-m-d H:i:s A”, $timeStamp);
return $myTime;
}
?>
Now we call the function to test the output.
<?php
// Let us first see the current time
echo ‘Current Time: ‘ . date(“Y-m-d H:i:s A”);
echo ‘<BR>’;
// Now let us add 2 hours and 10 minutes from now
echo ‘New Time: ‘ . addTime(2,10,0,0,0,0);
?>
Thus, this is so simple to add time using PHP.
So, I hope the people who were looking for a PHP example to add time and eventually came to my article on how to find local time would get the right article.
You can also download the PDF version of this article from the link below:
PDF version of PHP script to add time. Tutorial with code and example
PHP script/code to get Bangladeshi and any other local time
February 4, 2008
Often in local projects, the Bangladeshi developer may face problem in finding the local time since often the hosting is done in a server which is outside the country. Moreover, there are many different time formats like GMT, UTC etc. But a developer must use local time if it is included in the client’s requirements. Although a little bit browsing can help to solve the problem, I am sharing the solution with you so that you can re-use the code and save the browsing time. What is the benefit of re-inventing the wheel?
Now we can get the Bangladeshi time very easily if we add exactly only 6 (six) hours with GMT time. There may be other ways. But my solution solves it by adding 6 (six) hours with GMT time. Below is my simple function to get the Bangladeshi time in PHP.
<?php
function ShowBangladeshTime()
{
$hour = gmdate(“H”);
$minute = gmdate(“i”);
$seconds = gmdate(“s”);
$day = gmdate(“d”);
$month = gmdate(“m”);
$year = gmdate(“Y”);
// This is the offset from the server time to Bangladesh time.
$hour = $hour + 6;
return date(“h:i:s A Y-m-d”, mktime ($hour,$minute,$seconds,$month,$day,$year));
}
?>
You can also extract specific section of a time in this following way:
<?php
$dayOfTheWeek = date(“l”, mktime ($hour,$minute,$seconds,$month,$day,$year));
$hoursNow = date(“H”, mktime ($hour,$minute,$seconds,$month,$day,$year));
$minutesNow = date(“i”, mktime ($hour,$minute,$seconds,$month,$day,$year));
$AmPmNow = date(“A”, mktime ($hour,$minute,$seconds,$month,$day,$year));
?>
If you have to play with multiple country times, you can just add a switch case way like this:
<?php
function ShowSelectedCountryTime($country)
{
$hour = gmdate(“H”);
$minute = gmdate(“i”);
$seconds = gmdate(“s”);
$day = gmdate(“d”);
$month = gmdate(“m”);
$year = gmdate(“Y”);
switch($country)
{
case “Bangladesh”:
$hour = $hour + 6;
break;
case “India”:
$hour = $hour + 5;
$minute = $minute + 30;
break;
case “Nepal”:
$hour = $hour + 5;
$minute = $minute + 45;
break;
default:
$hour = $hour + 6;
}
return date(“h:i:s A Y-m-d”, mktime ($hour,$minute,$seconds,$month,$day,$year));
}
?>
It is simple to integrate such kind of simple function, script or code in your PHP class according to your requirements.
You can also download the PDF version of this article here below:
PHP script/code to get Bangladeshi and any other local time
How to encrypt PHP source code, software or project
December 7, 2007
There is a great software to encrypt PHP source codes. I used this software 2 (two) years ago and still now it is the best one that I recommend to everyone. It is SourceCop. Actually this software is for Windows platform. I found it by searching in http://www.google.com. I found many others also. But after trying all of them, I found this software the best one. This software is small, reliable and easy to use. If you give a little time, you can easily learn how to use this software.
Before we start, you need to download ad install the software. You can download the trial version from here:
http://www.sourcecop.com/download/sourcecop.exe
Now download the software in any location of your hard disk.
Installation:
Step 1: Double click on the executable file (i.e. sourcecop.exe) to initialize installation or you can also right click on the executable file and then single left click on the Open option. Now the installation process starts and you see a window as below. Click on the Next > button.

Step 2: Select I agree the agreement and click on the Next > button.

Step 3: At this point, browse your installation destination by clicking on the Browse button. By default, the destination is in the Program Files. If you want to change any settings, click o the Back > button. If you want to install in the Program Files folder, leave it as it is. Click on the Next > button.

Step 4: Select your Start Menu folder by clicking on the Browse button. If you want to change any settings, click o the Back > button. If you do not want to change this, leave it as it is and click on the Next > button.

Step 5: If you want to change any settings, click on the < Back button. Otherwise, click on the Install button to go ahead.

Step 6: The installation file starts copying necessary files. Please wait as long as it finishes copying files.

Step 7: Click on the Finish button and the installation process is complete.

Step 8: After the installation is complete, a desktop icon of the software becomes visible. It looks like as below:

How to use:
Go to your Desktop and open the software by double clicking or by right click and then click on the Open option. You can also start the software from the Start menu: Start -> All Programs -> SourceCop 3.0 -> SourceCop (in case of default installation). Now you will see the window shown below.

Now I will describe the features inside the PHP Project section.
- Select Source Folder button: Click on this button to link with the folder where you have kept your source codes. Actually these source codes will be encrypted. So, after the completion of the encryption process, file contents will be in new look but with the same functionality. Remember that the source codes inside this folder will be changed/converted/encrypted forever.
- Select Backup Destination button: Use this button to specify where to keep a backup copy of your source codes.
- File Types to Encrypt options: Select one or more of the file types that you want to encrypt.
- Any other extension: If you have written your code in any file whose file type is not like any of the previous types (i.e. .php, .php3, .php4, .phtml, .shtml, .inc), write the extension of that file such as .ihtml. Do not forget to give a .(dot) before the extension.
- Run script only on this IP address: Type the IP address only where you would allow executing your source codes. Actually this is specified when you want to give permission to someone for a single Server test run. You can do this also in case of showing your demo copy.
- Set an expiry date for script: Select a date to specify the time when your source codes will no more execute. This is a nice feature to stop the use of unlimited use of your codes. Be default, the date is a past date. Change it as per your requirements.
- Do not encrypt following files. Use comma to separate more than one files: Type one or more of the files that you do not want to be encrypted. You need to type the filename with the extension such as config.php
- Encrypt button: Now encryption process is ready to encrypt your files linked through the Select Source Folder button. Click on the Encrypt button and wait some time. The encryption process ends and gives you a message that the encryption was successful.
Recommendations and/or Suggestions:
1. It is very important to remember that the Select Source Folder button encrypts the source codes of the folder you link to it. So, you will not find the original code in this folder any more.
2. Do not get confused with the Select Source Folder and Select Backup Destination buttons. If you get confused and encrypt your only copy without a backup, you will lose your code forever.
3. Always use the Select Backup Destination button and thus keep a backup copy of your source code.
Important Notes:
Please remember that the software can take somewhat long time to encrypt a “big” project (i.e. a project with hundreds to thousands of files). So, give enough time to this software to encrypt your entire project. In other cases where the project is small to medium sized, the software encrypts the project’s PHP source codes almost immediately.
In some sites, you can find the full functional crack of this software. But I recommend you use a licensed version.