USING STRTOTIME() AND DATE() FUNCTION IN PHP
FETCHING DATE YEAR AND TIME SEPARATELY FROM USER INPUT
if (isset( $_POST[ 'submit' ] ) ) {
$date = $_POST['date_current'];
$time = strtotime($date);
$month = date("m",$time);
$year = date("Y",$time);
if($month>=1 && $month<=3){
$year = $year-1;
}
HERE FIRST WE HAVE TO UNDERSTAND THIS IS SIMPLE LOGIC TO RECEIVE SEPARATE DATE , MONTH , YEAR, AND TIME ALSO.
SUPPOSE WE HAVE TO GET DATA AS MONTH YEAR AND TIME AS SEPARATELY FOR SOME CALCULATION OR TO STORE IN SOME VARIABLES . IN SIMPLE YOU HAVE TO GET YEAR FROM USER ENTERED INPUT TAG BECAUSE IF YOU SELECT INPUT TYPE AS DATE THEN YOU WILL GET DATA MONTH AND DAY TOGETHER BUT FOR SOME REASON YOU NEED MONTH AND YEAR ONLY TO PERFORM CALCULATION
IN THIS LOGIC WE ARE RECEIVING DATE FROM FORM WHERE WE HAVE CREATED A INPUT TAG OF DATE TYPE FOR USER TO ENTER THE DATE, WHEN THE SUBMIT BUTTON IS PRESSED THEN
- WE HAVE STORED DATE IN A VARIABLE NAME $date FROM INPUT BY POST METHOD
- IN SECOND LINE $time IS USED TO STORED CONVERTED DATA WHICH IS CONVERTED INTO STRING BY USING strtotime() FUNCTION THIS FUNCTION CONVERTS DAY, MONTH, YEAR IN TO STRING AND THEN FROM STRING WE CAN FETCH DAY MONTH AND YEAR SEPARATELY
- HERE AN ANOTHER FUNCTION IS USED WHICH IS DATE("SPECIFIER","VARIABLE") IT EXTRACT DATE MONTHS AND YEAR SEPARATELY
- THEN WE STORE MONTH AND YEAR BY USING DATA FUNCTION , HERE 'M' 'Y' DENOTES SHORT FORM OF MONTHS AND YEAR
- STORE THE DATA IN VARIABLE AND THEN PERFORM THE DESIRED OPERATIONS