Añadir días a PHP Date en PHP
Tengo una fecha devuelta como parte de una consulta MySQL en la forma 2010-09-17
Me gustaría establecer las variables Dat Date2 a Dat Date5 de la siguiente manera:
$Date2 = $Date + 1
$Date3 = $Date + 2
Etc..
De modo que devuelve 2010-09-18
, 2010-09-19
etc...
He intentado
date('Y-m-d', strtotime($Date. ' + 1 day'))
Pero esto me da la fecha ANTERIOR $Date
.
¿Cuál es la forma correcta de obtener mis Fechas en el formato 'Y-m-d' para que puedan ser utilizadas en otra consulta?
9 answers
Todo lo que tienes que hacer es usar days
en lugar de day
así:
<?php
$Date = "2010-09-17";
echo date('Y-m-d', strtotime($Date. ' + 1 days'));
echo date('Y-m-d', strtotime($Date. ' + 2 days'));
?>
Y sale correctamente:
2010-09-18
2010-09-19
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-16 14:46:41
Si está utilizando PHP 5.3, puede utilizar un DateTime
objeto y su add
método:
$Date1 = '2010-09-17';
$date = new DateTime($Date1);
$date->add(new DateInterval('P1D')); // P1D means a period of 1 day
$Date2 = $date->format('Y-m-d');
Echa un vistazo a la DateInterval
constructor página de manual para ver cómo construir otros períodos para agregar a su fecha (2 días serían 'P2D'
, 3 serían 'P3D'
, y así sucesivamente).
Sin PHP 5.3, deberías poder usar strtotime
de la forma en que lo hiciste (lo he probado y funciona tanto en 5.1.6 como en 5.2.10):
$Date1 = '2010-09-17';
$Date2 = date('Y-m-d', strtotime($Date1 . " + 1 day"));
// var_dump($Date2) returns "2010-09-18"
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-16 14:57:10
A partir de PHP 5.2 puede usar modify con un objeto DateTime:
Http://php.net/manual/en/datetime.modify.php
$Date1 = '2010-09-17';
$date = new DateTime($Date1);
$date->modify('+1 day');
$Date2 = $date->format('Y-m-d');
Tenga cuidado al agregar meses... (y, en menor medida, años)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-02-18 01:06:49
Aquí hay un pequeño fragmento para demostrar las modificaciones de fecha:
$date = date("Y-m-d");
//increment 2 days
$mod_date = strtotime($date."+ 2 days");
echo date("Y-m-d",$mod_date) . "\n";
//decrement 2 days
$mod_date = strtotime($date."- 2 days");
echo date("Y-m-d",$mod_date) . "\n";
//increment 1 month
$mod_date = strtotime($date."+ 1 months");
echo date("Y-m-d",$mod_date) . "\n";
//increment 1 year
$mod_date = strtotime($date."+ 1 years");
echo date("Y-m-d",$mod_date) . "\n";
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-24 04:24:28
También puede utilizar el siguiente formato
strtotime("-3 days", time());
strtotime("+1 day", strtotime($date));
Puedes apilar los cambios de esta manera:
strtotime("+1 day", strtotime("+1 year", strtotime($date)));
Tenga en cuenta la diferencia entre este enfoque y el de otras respuestas: en lugar de concatenar los valores +1 day
y <timestamp>
, simplemente puede pasar la marca de tiempo como segundo parámetro de strtotime
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-06 14:41:06
Usando una variable para el Número de días
$myDate = "2014-01-16";
$nDays = 16;
$newDate = strtotime($myDate . '+ '.$nDays.'days');
echo newDate('d/m/Y', $soma); //format new date
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-06-18 08:25:26
Aquí está la solución más simple a su consulta
$date=date_create("2013-03-15");\\or your date string
date_add($date,date_interval_create_from_date_string("40 days"));\\add number of days
echo date_format($date,"Y-m-d");\\set date format of the result
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-21 18:41:51
Aquí tiene una manera fácil de resolver esto.
<?php
$date = "2015-11-17";
echo date('Y-m-d', strtotime($date. ' + 5 days'));
?>
La salida será:
2015-11-22
La solución ha encontrado desde aquí - Cómo Agregar Días hasta la fecha en PHP
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-22 18:46:04
Todos tienen que usar el siguiente código:
$nday = time() + ( 24 * 60 * 60);
echo 'Now: '. date('Y-m-d') ."\n";
echo 'Next Day: '. date('Y-m-d', $nday) ."\n";
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-12-14 11:08:36