Archive for category PHP

Find the duration in hours and minutes of two times using PHP

Another function that you can use to find the duration of any given two times. If you know PHP you can modify this further to include years, days & months (if you need help let me know!). But right now it will only say the time differences of two inputted times, which is actually what I need for now!

function duration($start,$end){
$tmp_e = explode(“:”,$end);
$tmp_s = explode(“:”,$start);
$start_time = mktime($tmp_s[0],$tmp_s[1],$tmp_s[2],8,8,8);
$end_time = mktime($tmp_e[0],$tmp_e[1],$tmp_e[2],8,8,8);
$durationt = $end_time-$start_time;
$hrs = intval(floor($durationt/3600));
$min = intval(floor($durationt/60))-($hrs*60);
if($hrs<>0){
$rval = $hrs.’ hr’;
}
if($min<>0){
if($hrs<>0){
$rval .= ‘ & ‘.$min.’ min’;
} else {
$rval = $min.’ min’;
}
}
return $rval;
}

How to use it:

$timeOne =’12:31′;

$timeTwo = ’15:43′;

echo duration($timeOne,$timeTwo); //output: 3 hours & 12 min

,

Leave a comment

Find the Saturday of any week using PHP!

When dealing with weeks, one problem that I face is the difference in week start day! For us the week start with Saturday but for the most parts in the rest of the world the week start with Sunday or Monday! So in PHP calculations and queries related with weeks, it is important to find the previous Saturday of a given day in a week! The following function will tell you the Saturday of any week, provided you give it any day within the week.

<?php
function wksaturday($day) //input format: d/m/yyyy
{
$tmp = explode(“/”,$day);
$dateob = mktime(0,0,0,$tmp[1],$tmp[0],$tmp[2]);
$toadd = -1-(date(‘w’,$dateob));
if($toadd<=-7){
$toadd += 7;
}
$dadate = mktime(0,0,0,$tmp[1],$tmp[0]+($toadd),$tmp[2]);
return date(‘d/m/Y’,$dadate);
}

?>

How to use it:

<?php

$mydate =’24/12/2008′;
echo “saturday: “.wksaturday($mydate); //output saturday: 20/12/2008

?>

,

Leave a comment

Add days to a given date in PHP

When dealing with dates it is necessary to add or subtract a given number of days to a date! I use the following little function to do it in PHP.

function dateadd($day,$toadd) //input format: d/m/yyyy
{
$tmp = explode(“/”,$day);
$dadate = mktime(0,0,0,$tmp[1],$tmp[0]+($toadd),$tmp[2]);
return date(‘d/m/Y’,$dadate);
}

It is a simple function that split the inputed date into an array and create a date object with the number of additional days included in the creation. And then format the out put!

How to use:

<?php
$mydate =’24/12/2008′;
echo dateadd($mydate,30); //output 23/01/2009
?>

,

4 Comments

Change date format in PHP

When coding in PHP & MySQL dealing with date is a huge headache if you are not prepared for it. As MySQL dates are in the format ‘yyyy-mm-dd’. However, we humans prefer date in the format of “dd/mm/yy” or “mm/dd/yy”! So when storing a date field to the database we need to change the date format to ymd, and after retrieving it from the database we need to change it to dmy or mdy.

To make life easier, it has been a while since I have used two functions for these changes. So here I share them, in case a newbie might need it.

function change2dmy($date) //input format: yyyy-m-d
{
$dtmp = explode(“-“,$date);
$dadate = mktime(0,0,0,$dtmp[1],$dtmp[2],$dtmp[0]);
return date(‘d/m/Y’,$dadate);
}
function change2ymd($date) //input format: d/m/yy or yyyy
{
$dtmp = explode(“/”,$date);
$dadate = mktime(0,0,0,$dtmp[1],$dtmp[0],$dtmp[2]);
return date(‘Y-m-d’,$dadate);
}

How to use it: it is a simple function.

<?php

$dmydate = ’24/12/08′
$ymddate = ‘2008-12-24’;

echo change2ymd($dmydate); //output: ‘2008-12-24′
echo change2dmy($ymddate); //output: ’24/12/2008’

?>

, ,

10 Comments

PHP & MySQL Tips: AZ List from DB

Past two days I have been busy designing a website! It is not too fancy from outside, but as usual all my websites are purely server based, with PHP and MySQL in the back end! So here is a tip for those PHP and MySQL fans! So if you are experienced in MySQL & PHP this can save you some time.

In a dynamic site AZ list can be tricky, as the users are allowed to add and edit the content of the website the AZ list contents can change at anytime.

azlist.jpg

In addition I don’t like to have links to pages which will have no contents, in other words if there is no resort starting with ‘U’, why have a link to it!

So here is the MySQL query I used:

“SELECT DISTINCT UPPER(LEFT(LName,1)) as letters FROM resorts ORDER BY letters”;

This is a simple query, here ‘LName’ is the name of the field and ‘resorts’ is the table in the database. So you will need a table named ‘resorts’ with at least one field names ‘LName’. So substitute as needed. This query will give you a list of unique first letters of all the data in the given fields!

azlist02.jpg

Now all you have to do is to loop the query result using PHP! Here is the complete coding!

//Database Connection String

$hostname = “server_localhost”;
$database = “database_name”;
$username = “user_name”;
$password = “password”;
$connection1 = mysql_pconnect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);

//Data Extraction

mysql_select_db($database, $connection1);
$query_AZList = “SELECT DISTINCT UPPER(LEFT(LName,1)) as letters FROM resorts ORDER BY letters”;
$AZList = mysql_query($query_AZList, $connection1) or die(mysql_error());
$row_AZList = mysql_fetch_assoc($AZList);
$totalRows_AZList = mysql_num_rows($AZList);

// Displaying the result by looping using PHP

<?php do { ?>
<?php echo ‘<a href=”list.php?ltr=’.$row_AZList[‘letters’].'”>’.$row_AZList[‘letters’].'</a>’; ?>
<?php } while ($row_AZList = mysql_fetch_assoc($AZList)); ?>

Thats it! Hope this will come in use for someone some day!

10 Comments