Tuesday, March 26, 2013

How Get Number of Days in a Month in PHP

Extracted from PHP.net

function days_in_month($month, $year)  { 
return $month == 2 ? 
($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : 
(($month - 1) % 7 % 2 ? 30 : 31); 
}

echo days_in_month(YOUR_MONTH,YOUR_YEAR);


More Simple way by a SafFunction

function days_in_month($month,$year) {
return date('t',mktime(0,0,0,$month,1,$year));
}

echo days_in_month(YOUR_MONTH,YOUR_YEAR);

Sunday, March 24, 2013

SafSendMailV2: A function to send mail using php

I started to make and collect my own function since 2001 and SafSendMail I made it in 2002 for a project where there was lots of mail sending option. If I want to send a mail I have to write whole lot and I didn't like it. Created SafSendMail so that I can send mail as I want using a single line.

Later I made SafSendMailV2 supporting HTML and other stuff as you can see, if any mail sent using V2 it will also record From-IP & From-File. Using this you can Identify different things.




function SafSendMailV2(
 $MYRECIPIENTE_EMAIL,
 $MYRECIPIENTE_NAME,
 $MYFROM_NAME,
 $MYFROM_EMAIL,
 $MYSUBJECT,
 $MYBODY,
 $MYHTML,
 $REMOTE_ADDR,
 $PHP_SELF){
 $MYRECIPIENTE_EMAIL  = "$MYRECIPIENTE_EMAIL";
 if ($MYHTML==true) {
  $MYHEADERS = "From: $MYFROM_NAME <$MYFROM_EMAIL>\r\n";
  $MYHEADERS .= "MIME-Version: 1.0\r\n";
  $MYHEADERS .= "Content-Type: text/html; charset=windows-1256\r\n";
  $MYHEADERS .= "Content-Transfer-Encoding: base64\r\n";
  $MYHEADERS .= "From-IP: $REMOTE_ADDR\r\n";
  $MYHEADERS .= "From-File: $PHP_SELF\r\n";
  $MYHEADERS .= chunk_split(base64_encode("$MYBODY"));
  mail("$MYRECIPIENTE_EMAIL", "$MYSUBJECT", "", $MYHEADERS);
 }else{
  $MYHEADERS = "From: $MYFROM_NAME <$MYFROM_EMAIL>\r\n";
  $MYHEADERS .= "From-IP: $REMOTE_ADDR\r\n";
  $MYHEADERS .= "From-File: $PHP_SELF";
  mail("$MYRECIPIENTE_EMAIL", "$MYSUBJECT", "$MYBODY", $MYHEADERS);
 }
}

How to use?
SafSendMailV2("Recipiente@domain.com","Recipiente Name","Your Name","Your Email","Subject",$MYBODY,true,$REMOTE_ADDR,$PHP_SELF)

How to Integrate Twitter into your Website



With jTweetsAnywhere, its very simple. Its powered by jQuery.

Step 1
Download the Zip package from here and extract them.

Step 2
Link these to your web page header.

<script src="javascripts/jquery.js" type="text/javascript"></script>
<script src="javascripts/jquery.jtweetsanywhere.js" type="text/javascript"></script>
<script src="javascripts/jtweetsanywhere-de-min.js" type="text/javascript"></script>
<link href="stylesheets/jquery.jtweetsanywhere.css" rel="stylesheet"></link>

Step 3
Set this to document.ready function

$(document).ready(function(){
    $('#your-twitter-div-id').jTweetsAnywhere({
        username: 'your-twitter-username',
        count: 5, //how many tweets you wants to show
        showTweetFeed: {
            /*showProfileImages: true,
            showUserScreenNames: true,
            showUserFullNames: true,*/
            showActionReply: true,
            showActionRetweet: true,
            showActionFavorite: true
  },
 });
});
You can customize the CSS file as you like. Simple, isn't it? :)

Saturday, March 23, 2013

JQuery Auto Refresh

Its small code but powerful and helpful.
 
<script>
 var auto_refresh = setInterval(
 function(){
  $('#autorefresh').fadeOut('slow').load('refresh.php').fadeIn("slow");
 }, 20000);
</script>

<div id="autorefresh">Hello</div>

ColdFusion like UUID in PHP



Originally extracted from php.net with minor changes

The main function of an UUID is being unique. UUIDs are commonly use in databases to store a unique string, but not limited to. This function will help you to create a Coldfusion like Universally Unique Identifier (UUID) in PHP. This was customized for ColdFusion UUID format (8-4-4-16) .

UUID is a mix of 0-9 A-F Hex value.


function GenerateUUID() {
return sprintf('%04x%04x-%04x-%04x-%04x%04x%04x%04x',
  // 32 bits for "time_low"
 mt_rand(0, 0xffff), mt_rand(0, 0xffff),
  // 16 bits for "time_mid"
 mt_rand(0, 0xffff),
  // 16 bits for "time_hi_and_version",
  // four most significant bits holds version number 4
  mt_rand(0, 0x0fff) | 0x4000,
  // 16 bits, 8 bits for "clk_seq_hi_res",
  // 8 bits for "clk_seq_low",
  // two most significant bits holds zero and 
         // one for variant DCE1.1
  mt_rand(0, 0x3fff) | 0x8000,
  // 48 bits for "node"
  mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
 );
}


Friday, March 22, 2013

PHP Date Functions with SafDate Functions

This is a set of small functions I use, this will make my life easy, no need to remember all the variables at date() function and you can make some predefined functions that you use always.













saf.date.functions.php
<?php
//Pass 2007-01-22 as date and output  as January 22, 2007
function SafDate1V2($date){
    list($y,$m,$d) = explode("-",$date);
    $mycooldate = date("F d, Y",mktime(0, 0, 0, $m, $d, $y));
    return $mycooldate;
}
//Pass 2007-01-22 as date and output  as Jan 22, 2007
function SafDate2V2($date){
    list($y,$m,$d) = explode("-",$date);
    $mycooldate = date("M d, Y",mktime(0, 0, 0, $m, $d, $y));
    return $mycooldate;

}
//Pass 2007-01-22 as date and output  as Jan 22, 07
function SafDate3V2($date){
    list($y,$m,$d) = explode("-",$date);
    $mycooldate = date("M d, y",mktime(0, 0, 0, $m, $d, $y));
    return $mycooldate;
}
//Pass 2007-01-22 as date and output as Sunday Jan 22, 2007
function SafDate4V2($date){
    list($y,$m,$d) = explode("-",$date);
    $mycooldate = date("l M d, Y",mktime(0, 0, 0, $m, $d, $y));
    return $mycooldate;
}
?>
<?php
//Pass 2007-01-22 as date and output as Sun, Jan 22, 2007
function SafDate5V2($date){
    list($y,$m,$d) = explode("-",$date);
    $mycooldate = @date("D, M d, Y",mktime(0, 0, 0, $m, $d, $y));
    return $mycooldate;
}
?>

Wednesday, March 20, 2013

Beginning - MyCodeGenius

MyCodeGenius is a blog space of our day-to-day code snippets which we use in our programming works that we would like to share with others.

Feel free to ask anything and we'll try our best to help as long as the time permits.

Happy Coding.

Let's start with a meme. :D


Source : www.memecenter.com