PHP7 date manipulation

Add days to a date

$date      =  "2019-12-20";
$newdate = date("Y-m-d",strtotime($date."+  5 day"));
-->2019-12-25

Take, day, month, etc.

$date      =  "2019-12-20";
// get day
$daysmonth  =  date("d", strtotime($date));
--> 20
// get month
$montmonth  =  date("m", strtotime($date));
--> 06
// get year
$yearmonth  =  date("Y", strtotime($date));
--> 2019
// get 3 letters day name
$week =  date("D", strtotime($newdate));
--> Wed
// take complete day name 
$week =  date("l", strtotime($newdate));
--> Wednesday
// get 3 letter from month name 
$week =  date("M", strtotime($newdate));
--> Des
// get month name
$week =  date("M", strtotime($nuevafecha));
--> December

Add the date and subtract

// get date
$datevar = date("Y-m-d H:i:s");
--> 2019-05-25 13:02:12

// substract 20 hours 
date ('d-m-Y H:i:s', strtotime ('- 20 hour', strtotime($datevar)));
--> 24-05-2019 17:02:12

// add 5 hours
date ('d-m-Y H:i:s', strtotime ('+ 5 hour', strtotime($datevar)));
--> 25-05-2019 18:02:12

// substract 8 minutes
date ('d-m-Y H:i:s', strtotime ('- 8 minute', strtotime($datevar)));
---> 25-05-2019 12:54:12

// add 16 minutes
date ('d-m-Y H:i:s', strtotime ('+ 16 minute', strtotime($datevar)));
--> 25-05-2019 12:54:12

// substract 40 seconds
date ('d-m-Y H:i:s', strtotime ('- 40 second', strtotime($datevar)));
--> 25-05-2019 13:01:32

// add 8 seconds
date ('d-m-Y H:i:s', strtotime ('+ 8 second', strtotime($datevar)));
--> 25-05-2019 13:02:20

get the first and last day of the month

//$mes = date("Y-m");
$mes = $month;

//get last day of the month 
$daylast =  date("Y-m-d", strtotime("last day of ".$mes));

//get the first day of the month
$fecha      =  date("Y-m-d", strtotime("first day of ".$mes));

Get the date of Monday of the week

//Get monday of the first week
$fecha      =  "2019-06-20";
$daysmonth  =  date("d", strtotime($fecha));
$montmonth  =  date("m", strtotime($fecha));
$yearmonth  =  date("Y", strtotime($fecha));
$nuevaFecha =  mktime(0,0,0,$montmonth,$daysmonth,$yearmonth);
$diaDeLaSemana = date("w", $nuevaFecha);
$nuevaFecha = $nuevaFecha - ($diaDeLaSemana*24*3600); 
$dateini = date ("Y-m-d",$nuevaFecha);
--> 2019-06-16


Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *