Home
 

Image Resize on the fly

This code can be used to re-size any image very quickly. This is the easiest process to re-size image.

Click here to download source code

From the source code you will get two files named ‘image.php’ and ‘index.php’.

Suppose you want to resize an image Desert.jpg on the fly then you can use below code.

1
<img src="image.php?w=241&h=101&f=Desert.jpg" border="0" alt=" " />  

to the section where you want to show the resized image.

 

Find Date of n days or n months or n years later from today using PHP

Current Date

1
2
3
$today =  date("Y-M-d");
echo "Today's date is: " . $today. "<br />";

OUTPUT :

Today’s date is: 2011-May-03

Two days later from today

1
2
3
4
5
$expire_date =mktime(0,0,0,date("m"),date("d")+2,date("Y"));
$lastdate=date("Y/M/d", $expire_date);
echo "Two days later from today= ".$lastdate;

OUTPUT would be like this:

1
Two days later from today= 2011/May/05

Two months later from today

1
2
3
4
5
$expire_date =mktime(0,0,0,date("m")+2,date("d"),date("Y"));
$lastdate=date("Y/M/d", $expire_date);
echo "Two months later from today= ".$lastdate;

OUTPUT would be like this:

1
Two months later from today= 2011/Jul/03

Two years later from today

1
2
3
4
5
$expire_date =mktime(0,0,0,date("m"),date("d"),date("Y")+2);
$lastdate=date("Y/M/d", $expire_date);
echo "Two years later from today= ".$lastdate;

OUTPUT would be like this:

1
Two years later from today= 2013/May/03


Inserts HTML line breaks before all newlines in a string

This code is very useful when we insert large content with number of new lines in the database table.

In case of html line, it inserts <br /> for all \n.

1
2
3
4
5
<?php
echo nl2br("Hello \n World");
?>

OUTPUT will be

1
2
3
Hello
World

How to Find Date of Tomorrow, Next Month, Next Year using PHP

Another interesting and effective way to find date of certain day, month or year from today.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
     // TODAY'S DATE
     $today =  date("m-d-Y");
	 echo "Today's date is: " . $today. "<br />";

 	// 19 DAYS FROM TODAY
     $somedays = date("m-d-Y",strtotime("+19 days"));
	 echo "Date after 19 days from today: ". $somedays. "<br />";

     // 1 MONTH FROM TODAY
     $onemonth = date("m-d-Y",strtotime("+1 months"));
	 echo "Date after 1 month from today: " .$onemonth. "<br />";

     // 1 YEAR FROM TODAY
     $oneyear = date("m-d-Y",strtotime("+1 years"));
 	echo "Date after 1 year from today: ".$oneyear."<br />";
?>

Output would be like this.

1
2
3
4
5
6
7
Today's date is: 05-01-2011
Date after 19 days from today: 05-20-2011
Date after 1 month from today: 06-01-2011
Date after 1 year from today: 05-01-2012

Worldpay Integration – Payment Response (Callback) – PHP Code

Recently we encountered an issue with Worldpay Integration. When we searched in Google we couldn’t find any document which can guide one, to properly setup payment call back response. Its actually very simple and should be done in 10 minutes.

Step 1:

Download the file worldpay. This code is taken from worldpay knowledgebase section.

1
<input type="hidden" name="instId" value="xxxxxx">

You need to change the instID value to your installation id which you can get from the Step 2 below.

Step 2:

First login at worldpay test environment. The url should be this. After login go to Installation link from left bar. On the opened page you should see your installation id.

Now click on the Test-Integration setup icon. From next screen click on “Payment Response URL“. There put http://www.yourdomain.tld/response.php.

We’ll create response.php in Step 3 section of the tutorial. Now tick “Enable the Shopper Response“. In response.php we’ll show custom payment response which worldpay doesn’t show by default. To show those values to your customer you need to create and setup response.php properly. Since this would be a custom php page, so you can write your own custom php code to insert those values into database (This is not possible with default worldpay response).

Step 3:

Now create response.php and paste code below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<html>
<head><title>Thank you for your payment</title></head>
<WPDISPLAY FILE=header.html>
Worldpay Transaction id - <?php echo $_POST["transId"]; ?><br />
Payment Status - <?php echo $_POST["transStatus"]; ?><br />
Transaction Time - <?php echo $_POST["transTime"]; ?><br />
Amount - <?php echo $_POST["authCurrency"]; ?> <?php echo $_POST["authAmount"]; ?>.<br />
IP Address - <?php echo $_POST["ipAddress"]; ?>
<WPDISPLAY ITEM=banner>
<WPDISPLAY FILE=footer.html>
</html>

Now let me explain the code. Below 3 lines are default payment template of worldpay, which calls header, worldpay banner and footer. You can keep this intact, so that your customer can understand that they paid via WorldPay.

<WPDISPLAY FILE=header.html>
<WPDISPLAY ITEM=banner>
<WPDISPLAY FILE=footer.html>

WorldPay returns various parameters which can be captured by _POST command.

1
2
3
4
5
6
7
8
9
Worldpay Transaction id - <?php echo $_POST["transId"]; ?><br />
Payment Status - <?php echo $_POST["transStatus"]; ?><br />
Transaction Time - <?php echo $_POST["transTime"]; ?><br />
Amount - <?php echo $_POST["authCurrency"]; ?> <?php echo $_POST["authAmount"]; ?>.<br />
IP Address - <?php echo $_POST["ipAddress"]; ?>
You can find more parameters at http://www.rbsworldpay.com/support/kb/bg/htmlredirect/rhtml5904.html, by using various parameters, you can easily customize as per your requirement.

Php code to find date after n days from today

We often face problem in finding date of say after 40 days from today. Since different month has different total days, so finding exact date is a challenge. For example January has 31 days, February has 28/29 days, April has 30 days. To sort out the problem you can follow below codes.

1
2
3
4
5
<?php
echo date("m/d/y");</span>
?>

would give you date in format 04/30/11 (04 is month April, 30 is date and 11 is year 2011)

Asian countries can use

1
<?php echo date("d/m/y"); // date/month/year format. ?>

Suppose we want to find date after 60 days from today (1st may 2011). To do this follow the code

1
2
3
<?php $tomorrow = mktime(0, 0, 0, date("m"), date("d")+60, date("y"));
echo "The date is ".date("d/m/y", $tomorrow); ?>

OUTPUT will be

1
The date is 30/06/11. (30 June 2011)

Now let’s do a little experiment. June has 30 days so let’s see the output of below code

1
2
3
<?php $tomorrow = mktime(0, 0, 0, date("m"), date("d")+61, date("y"));
echo "The date is ".date("d/m/y", $tomorrow); ?>

OUTPUT will be

1
The date is 01/07/11. (1 July 2011).

So it means by using mktime() and date() function of php we can find any date in future and we don’t have to worry about number of days in a month, leap-year.

 
 
 

Categories

Tag