Find Date using php

Find Date using php

PHP is open source software we can find date using php is very easy. Date function is used to find current date. 

PHP Code


$current_date = date('Y-m-d'); 
$timestampFirstDay = strtotime('last monday');

if (date('Y-m-d', $timestampFirstDay) == date('Y-m-d', time() - 7*24*3600)) {
    // we are is the same day, add one week to current date
    $timestampFirstDay += 7 * 24 * 3600;
} 

$first_day_lastweek = date('Y-m-d',($timestampFirstDay - 7 * 24 * 3600));
$last_day_lastweek = date('Y-m-d',($timestampFirstDay - 1 * 24 * 3600)); 

$first_day_week = date('Y-m-d', $timestampFirstDay);
$last_day_week = date('Y-m-d',($timestampFirstDay += 6 * 24 * 3600)); 

$first_day_nextweek = date('Y-m-d',($timestampFirstDay += 1 * 24 * 3600));
$last_day_nextweek = date('Y-m-d',($timestampFirstDay += 6 * 24 * 3600));

$first_day_month = date('Y')."-".date('m')."-01";
$last_day_month = lastday(date('m'),date('Y')); 

$first_day_lastmonth = get_previous_month(date('Y')."-".date('m')."-01");
$last_day_lastmonth = lastday(FormatDate($first_day_lastmonth,'m'),FormatDate($first_day_lastmonth,'Y')); 

function lastday($month,$year) {
   if (empty($month)) {
      $month = date('m');
   }

   if (empty($year)) {
      $year = date('Y');
   }

   $result = strtotime("{$year}-{$month}-01");
   $result = strtotime('-1 second', strtotime('+1 month', $result));
   return date('Y-m-d', $result);
} 

function get_previous_month($date) {
   $date = str_replace("/", "-", $date);
   $year=date("Y",strtotime($date));
  $month=date("n",strtotime($date)) - 1;
  if ($month == 0){
     $month = 12;
     $year = $year - 1;
  }
  return date("Y-m-d", mktime(0, 0, 0, $month, 1, $year));
} 

function FormatDate($MyDate,$dateFormat){
$date_array = explode("-",$MyDate); // split the array
$var_year = $date_array[0];
$var_month = $date_array[1];
$var_day = $date_array[2];
  switch ($dateFormat) {
  case "dmy" :
     $var_Dt =  $var_day."-".$var_month."-".$var_year;
     return($var_Dt);

  case "ymd" :
    $var_Dt =  $var_year."-".$var_month."-".$var_day;
    return($var_Dt);

  case "mdy" :
    $var_Dt =  $var_month."-".$var_day."-".$var_year;
    return($var_Dt);

  case "MY" :
    $var_Dt =  date('F', mktime(0, 0, 0, $var_month))."-".$var_year;
    return($var_Dt);

case "DMY" :
  $var_Dt =  $var_day."-".substr(date('F', mktime(0, 0, 0, $var_month)),0,3)."-".$var_year;
  return($var_Dt);

case "D" :

  $var_Dt =  $var_day;

  return($var_Dt); 

case "M" :
  $var_Dt =  strtoupper(substr(date('F', mktime(0, 0, 0, $var_month)),0,3));
  return($var_Dt); 

case "m" :
  $var_Dt =  $var_month;
  return($var_Dt);  

case "Y" :
  $var_Dt =  $var_year;
  return($var_Dt);  

default :
  $var_Dt =  $var_day."-".substr(date('F', mktime(0, 0, 0, $var_month)),0,3)."-".$var_year;
  return($var_Dt);
  }

}

HTML Code

<html>
<head>
   <title>Find Date</title>
</head>
<body>
<table cellpadding="2" cellspacing="0" border="1" width="30%">
<tr>
<td>Today</td>
<td><?php echo $current_date;?></td>
</tr>
‚Äč
<tr>
<td>First Day Last Week</td>
<td><?php echo $first_day_lastweek;?></td>
</tr>

<tr>
<td>Last Day Last Week</td>
<td><?php echo $last_day_lastweek;?></td>
</tr>

<tr>
<td>Week First Day</td>
<td><?php echo $first_day_week;?></td>
</tr>

<tr>
<td>Week Last Day</td>
<td><?php echo $last_day_week;?></td>
</tr>

<tr>
<td>Next Week First Day</td>
<td><?php echo $first_day_nextweek;?></td>
</tr>

<tr>
<td>Next Week Last Day</td>
<td><?php echo $last_day_nextweek;?></td>
</tr>

<tr>
<td>Month First Day</td>
<td><?php echo $first_day_month;?></td>
</tr>

<tr>
<td>Month Last Day</td>
<td><?php echo $last_day_month;?></td>
</tr>

<tr>
<td>First Day Last Month</td>
<td><?php echo $first_day_lastmonth;?></td>
</tr>

<tr>
<td>Last Day Last Month</td>
<td><?php echo $last_day_lastmonth;?></td>
</tr>

</table>
</body>
</html>

Download sour code click Here

0 Comments

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed

>