$date = date_create['2013-10-27'];// This is the date that inputed in textbox and that format is [Y-m-d]
$date = date_create['2013-10-10'];// and if i click the button i want to force change the 27 to 10?
Should i use date_modify and do some loop or there's other way to change it in easy way rather than looping.
asked Oct 3, 2013 at 2:56
1
$in = date_create['2013-10-27'];
// example 1
$out = date_create[$in->format['Y-m-10']];
echo $out->format['Y-m-d'] . "\n";
// example 2
$out = clone $in;
$out->setDate[$out->format['Y'], $out->format['m'], 10];
echo $out->format['Y-m-d'] . "\n";
// example 3
$out = clone $in;
$out->modify[[10 - $out->format['d']] . ' day'];
echo $out->format['Y-m-d'] . "\n";
Demo.
Martijn
15.5k4 gold badges36 silver badges66 bronze badges
answered Oct 3, 2013 at 7:49
GlavićGlavić
41.7k13 gold badges74 silver badges107 bronze badges
1
You can use the native PHP "date_date_set" function to make this change.
$date = date_create['2013-10-27'];
echo $date->format['Y-m-d'];
2013-10-27
date_date_set[$date,
date_format[$date, 'Y'],
date_format[$date, 'm'],
10];
echo $date->format['Y-m-d'];
2013-10-10
Or using the Object-Oriented style:
$date = new DateTime['2013-10-27'];
echo $date->format['Y-m-d'];
2013-10-27
$date->setDate[$date->format['Y'], $date->format['m'], 10];
echo $date->format['Y-m-d'];
2013-10-10
answered Jan 22, 2017 at 19:15
$date = date["Y-m-d", strtotime["2013-10-10"]];
Updated: to force to change the day from 27 to 10
1] get the year and month
$date = date["Y-m-", strtotime[ $_POST['user_selected_date'] ]];
2] append your day
$date .= '10';
Also you can finish it in ONE step $date = date["Y-m-10", strtotime[ $_POST['user_selected_date'] ]];
answered Oct 3, 2013 at 2:58
Allen ChakAllen Chak
1,6591 gold badge9 silver badges21 bronze badges
2
Note: If you're just trying to modify the value of a day on the date that comes from a
submitted from a
. You could try these steps:
$date = '2013-10-27'; // pass the value of input first.
$date = explode['-', $date]; // explode to get array of YY-MM-DD
//formatted results of array would be
$date[0] = '2013'; // YY
$date[1] = '10'; // MM
$date[2] = '17'; // DD
// when trigger a button to change the day value.
$date[2] = '10'; // this would change the previous value of DD/Day to this one. Or input any value you want to execute when the button is triggered
// then implode the array again for datetime format.
$date = implode['-', $date]; // that will output '2013-10-10'.
// lastly create date format
$date = date_create[$date];
Bono
4,6896 gold badges46 silver badges76 bronze badges
answered Oct 3, 2013 at 3:14
Jhonathan H.Jhonathan H.
2,7121 gold badge17 silver badges26 bronze badges
1
Use PCRE Function
$date = '2013-10-27';
$new_date = preg_replace["/\d{2}$/", "10", $date];
preg_replace manual
answered Oct 3, 2013 at 4:37
1