Showing posts with label date. Show all posts
Showing posts with label date. Show all posts

Thursday, November 26, 2009

Javascript Date Validation / Comparison

Problem:
User enters two dates, say start date and end date, then we need to alert him if the start date is greater than end date.

Solution:
Assume we are getting the date to the input field using a datepicker.

First we have to convert the string into Date() object using
var dObj = new Date(year, month, date); (also note: the month value range from 0 to 11)
and we can compare the dates.

Example:
var x = document.formName;

//assume the date format is YYYY/MM/DD
var sDate1 = x.start_date.value;
var sDate = new Date(sDate1.split('/')[0], parseInt((sDate1.split('/')[1])-1), sDate1.split('/')[2]);
var eDate1 = x.end_date.value;
var eDate = new Date(eDate1.split('/')[0], parseInt((eDate1.split('/')[1])-1), eDate1.split('/')[2]);
if(eDate >= sDate)
{
    alert("End date is greater than start date; please try again....");
    return false;
}

-

Thursday, November 19, 2009

How to add month, days, years in a date

<?php
$day1 = "2008/10/20";

//add month
$day2 = date("Y/m/d", strtotime("+2 months", strtotime($day1)));
//"2008/12/20"
$day2 = date("Y/m/d", strtotime("+3 months", strtotime($day1)));
//"2009/01/20"
//add days
$day2 = date("Y/m/d", strtotime("+5 days", strtotime($day1))); //"2008/10/25"
$day2 = date("Y/m/d", strtotime("+20 days", strtotime($day1))); //"2008/11/09"
//years
$day2 = date("Y/m/d", strtotime("+2 years", strtotime($day1))); //"2010/10/20"
?>
Also we can add weeks.

Friday, October 10, 2008

how to change month number to month name?

how to change month no to month name?
link 02 to february or Feb...

function month_name($month_no)
{
$timestmp = mktime(0,0,0,$month_no,1,2008);
return date("M",$timestmp);
}

echo month_name(10); //will return October
?>


Cheers!

Friday, May 23, 2008

No of days between two dates in php

We can find the no days between two dates using PHP....
There are 2 methods:

1) Split the date and subtract
$date1 = "2008-05-18";
$arr = explode('-',$date1); //split the date
$date2 = $arr[2]; //stores 18
$cur_date = date("d"); // current date
echo "Difference: ";
echo $cur_date-$date2;



2) Convert dates into timestamp, subtract and then convert as we want
$date1 = "2008-04-18";
$to_day = date("Y-m-d"); //current date
$day_diff = (strtotime($to_day)-strtotime($date1))/86400; //60*60*24=86400
echo $day_diff;

//change the divider(864000 to get ur corresponding difference
$hour_difference = (strtotime($to_day)-strtotime($date1))/3600; //60*60=3600
$week_diff = (strtotime($to_day)-strtotime($date1))/604800; //60*60*24*7=604800