Thursday, July 29, 2010

Removing special characters from a string using php

$str = '"!@#$%^&*()MyTest\'_+-|/\/{}[];:<,>.?';
$tempVar = preg_replace('/[^a-zA-Z0-9]*/','',$str);
echo $tempVar;

Wednesday, July 21, 2010

get start and end date according given date and duration

function getEndStartDate($date, $duration) {//date format yyyy-mm-dd
$date_time = strtotime( $date );

$date_start_time = mktime( 0, 0, 0, date('m', $date_time), date('d', $date_time), date( 'Y', $date_time ) );
$date_start = date( 'Y-m-d 00:00:00', $date_start_time );
$date_end = date( 'Y-m-d 23:59:59', strtotime( '+'.$duration.' month -1 day', $date_start_time ) );

return array( 'date_start' => $date_start, 'date_end' => $date_end );}

get start and end date for a week

/**
* This function is used for finding start and end date for a week
*/
function week_from_monday($date) {
// Assuming $date is in format DD-MM-YYYY
list($day, $month, $year) = explode("-", $date);

// Get the weekday of the given date
$wkday = date('l',mktime('0','0','0', $month, $day, $year));

switch($wkday) {
case 'Monday': $numDaysToMon = 0; break;
case 'Tuesday': $numDaysToMon = 1; break;
case 'Wednesday': $numDaysToMon = 2; break;
case 'Thursday': $numDaysToMon = 3; break;
case 'Friday': $numDaysToMon = 4; break;
case 'Saturday': $numDaysToMon = 5; break;
case 'Sunday': $numDaysToMon = 6; break;
}

// Timestamp of the monday for that week
$monday = mktime('0','0','0', $month, $day-$numDaysToMon, $year);

$seconds_in_a_day = 86400;

// Get date for 7 days from Monday (inclusive)
for($i=0; $i<7; $i++)
{
$dates[$i] = date('Y-m-d',$monday+($seconds_in_a_day*$i));
}

return $dates;
}//End week_from_monday

how we can get number of occurance of a value in an array

#array_search_match($needle, $haystack) returns all the keys of the values that match $needle in $haystack
function array_search_all($needle, $haystack){
foreach ($haystack as $k=>$v) {
if($haystack[$k]==$needle){
$array[] = $k;
}
}
return ($array);
}