Friday, 3 May 2013

JavaScript Basics

 JavaScript Array:

  Here i am going to Guide you on JavaScript Arrays. Array in JS is Same as in PHP and other Programming Languages.    Here is the Syntax for creating Array,

Syntax:

var fruitArray = new Array( "apple", "orange", "mango","banana" );
OR
var fruitArray = new Array[ "apple", "orange", "mango","banana" ];

 In this Array,

  fruitsarray[1] is the First element,

 fruitsarray[2] is the Second element,

 fruitsarray[3] is the Third element,

 fruitsarray[4] is the Fourth element.

Dates:           

new Date( ):

        This will display Current Date with Time. But We Programmers won't like to display Dates with time. But we will format it with some functions and display it.       
Here is a program to Display current date As many clients wants to display in Web Pages.

Function to display Current Date:  

function displayTodaysDate()
{
var x = new Array("Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday");
var z = new Array("January","February","March","April", "May","June",
"July", "August", "September","October","November","December");

var d = new Date();
var day = d.getDay();
var month = d.getMonth();

// Assemble the string to display
var s = x[day] + ", " + d.getDate() + " " + (z[month]) + " " + (d.getFullYear());

document.write(s);
}

Output:  Friday, 3 May 2013

 Just Copy it in your Page and then Call this function using the below code,

<script type="text/javascript">

displayTodaysDate();

</script>

Other Syntax:

                Please Use any of the below syntax below to get desired date output in javascript
new Date(milliseconds)
new Date(datestring) new Date(year,month,date[,hour,minute,second,millisecond ])

PHP function to find dates between two dates

PHP function to find dates between two dates

      Here i am going to write a function for finding dates between two Dates using PHP. It will output all the days between two given dates as a array. The main feature of this is,
      ->It will output all days in array format
    You can use this function to find number of days between given two dates as below.

Displaying all days:

            To display all days, just write the below code
<?php

                 var_dump(createDateRangeArray("2011-02-02","2013-02-02"));                 
?>

Displaying Number of days:

          To display number of days between these two dates just use the below code
count(createDateRange("2011-02-02","2013-02-02"));
?>

Main Function:              

 Here is the function to do all the above required things. Just copy it in your PHP file and display dates as i specified above.  
<?php 

function createDateRange($strDateFrom,$strDateTo) {
//two dates formatted as YYYY-MM-DD and creates an
$aryRange=array();
$iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2),     substr($strDateFrom,8,2),substr($strDateFrom,0,4));     
iDateTo=mktime(1,0,0,substr($strDateTo,5,2),     substr($strDateTo,8,2),substr($strDateTo,0,4));
 if ($iDateTo>=$iDateFrom)     {        
array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry         
while ($iDateFrom<$iDateTo)         {             
$iDateFrom+=86400; // add 24 hours             
array_push($aryRange,date('Y-m-d',$iDateFrom));        
 }     }     return $aryRange; 
}  ?>