Thursday, 9 November 2017

Change Date Format From YYYY-MM-DD to DD-MM-YYYY also getting yesterday date for particular date in PHP. Retriving the day record from MySql

//////////////////////////////// GET LAST DATE OR YESTERDAYS /////////////////////////////////////////////////////////

$dt=date('d/m/Y');      // Gives todays date in dd/mm/yyyy format let it be my today's post publishing date i.e., 09/11/2017
 
$lastdate=0;               // Lets assusme lastdate is zero, as let x=0 
 
$vardate = str_replace('/', '-', $dt);   /* To convert the format we should first change our date as only digits, i.e., without special symbols  now vardate=09112017 */
 
$formatdt=date("Y-m-d",strtotime($vardate));  /* Now formatdt = 2017-11-09, dont miss strtotime function, missing it gives weird result such as 1970-01-01. */
 
$prevdt=DateTime::createFromFormat('Y-m-d',$formatdt);  /* Create a DateTime object, I tried several ways like date('d/m/Y',strtotime("-1 days"))  etc., but nothing worked well. This procedure delivered perfect result */
 
$prevdt->modify('-1 days'); 
/* Now we have current date in our object , we can do whatever we want like 
$start_date = '2013-03-06';
$date = DateTime::createFromFormat('Y-m-d',$start_date);

$date->modify('+1 month');
echo $date->format('Y-m-d');//2013-04-06

$date->modify('+4 year');
echo $date->format('Y-m-d');//2017-04-06

$date->modify('+6 day');
echo $date->format('Y-m-d');//2017-04-12

$date->modify('+24 hours');
echo $date->format('Y-m-d');//2017-04-13

$date->modify('-7 years');
echo $date->format('Y-m-d'); //2010-04-13

$date->modify('-18 months');
echo $date->format('Y-m-d'); //2008-10-13 
etc. 
 
Here our case is to get previous date so I used '-1 days' . Hope you understood */

$yesterdaydate=$prevdt->format('Y-m-d');  /* Obtaining previous date in yyyy-mm-dd format for database query retrieval purpose, Mysql date format is yyyy-mm-dd H:M:S */

$ydt=date('d/m/Y',strtotime($yesterdaydate));  // Converting previous date our reading format i.e., dd/mm/yyyy
 
$chkexists = mysql_result(mysql_query("SELECT IFNULL(MAX(id),0) FROM repdone WHERE tdate='$ydt'"),0);  /* Checking whether database entry exists in selected table on this particular date. This is case of daily reports. */
 
if($chkexists == 0){
  // CASE PREVIOUS DAY IS HOLIDAY FOR SELECTED DATE
 
  $chkdt=date('Y-m-d H:i:s',strtotime($yesterdaydate));
 /* GETTING previous date in mysql date format, note that we are using $yesterdaydate variable which is in yyyy-mm-dd format previous date, this format is best acceptable format for all coding languages but the users need readable format i.e., dd-mm-yyyy or dd/mm/yyyy format. That's why both formats are stored in separate variables as well as both formats are stored in database also. */

  $lastdate = mysql_result(mysql_query("SELECT IFNULL(MAX(repdate),0) FROM dailystatistics where repdate <= '$chkdt'"),0);
   // Getting Last maximum date  below the selected date from database through query
 
if($lastdate==0) {
  // In case database query fails. This code doesn't execute but for safe side it is written due to date format problems
 
  $chkrep = mysql_result(mysql_query("SELECT IFNULL(id,0) FROM repdone WHERE tdate='$dt'"),0); 
  /* If Todays  Record inserted then the value is 1 to get yesterdays date otherwise zero gives max id for yesterdays date */
  $lastrepid =  mysql_result(mysql_query("SELECT IFNULL(MAX(id),0) FROM repdone where id<$chkrep"),0);  // Getting just below id of selected date record
  if($lastrepid > 0) {
      $lastdate=mysql_result(mysql_query("SELECT IFNULL(tdate,0) FROM repdone where id='$lastrepid'"),0); // getting the date of the previous record id
  }
}

if($lastdate!=0)
  $vardate = str_replace('/', '-', $lastdate);  // Again for converting the date to dd/mm/yyyy format we need to replace the special symbols
  $ydt=date("d/m/Y",strtotime($vardate));  // Now we have previous date of selected date ($dt) in format dd/mm/yyyy is in $ydt variable
 
 } // End of chkexists


////////////////////////////////////////////////////////////////////////////////////////////////////////