Trouble with dates in php - php

I want to print the date every three months before one year past,from the date selected by user, but when i made the increment the loop breaks and only prints the fist date, could help me, please
this is my code in PHP
$fecha = date('m/d/Y',strtotime($_POST['fecha']));
printf( date('d/m/Y',strtotime($fecha)).'----');
$fin = date('m/d/Y',strtotime($fecha."+ 1 year"));
printf(date('d/m/Y',strtotime($fin)).'----');
if($fecha<$fin){
$fecha = date('m/d/Y',strtotime($fecha."+ 1 month"));
printf( date('d/m/Y',strtotime($fecha)));
}
<form method="POST" action="./fechas.php">
<input type="date" name="fecha" id="fecha">
<input type="submit" value="enviar">
</form>
This is the output:
And i want to print this:
15/10/2020----
15/11/2020----
15/12/2020---
...

You're repeatedly reformatting and reparsing dates and intervals with the most unreliable functions when there's an entire DateTime library to make your life easier.
$input = new DateTime('2020-09-24', new DatetimeZone('UTC'));
$past = (clone $input)->sub(new DateInterval('P1Y'));
$period = new DateInterval('P3M');
$cur = clone $past;
do {
echo $cur->format('Y-m-d') . "\n";
$cur->add($period);
} while( $cur <= $input );
Output:
2019-09-24
2019-12-24
2020-03-24
2020-06-24
2020-09-24

Related

Get day from array of dates via html form

I have a form which submits an array of fields e.g:
<input class="form-control" type="date" name="schedule-date[]">
<input type="text" class="form-control" name="schedule-start[]">
<input type="text" class="form-control" name="schedule-end[]">
with php I am trying to get the day of the week from the date foreach so far I only get the last day. Here's my code.
$date = $_POST['schedule-date'];
foreach($date as $d){
$day = date("l", strtotime($d));
}
$start = $_POST['schedule-start'];
$end = $_POST['schedule-end'];
foreach( $date as $key => $n ) {
echo $date[$key]." ".$day." "$start." ".$end;
echo ;
}
I get:
2018-12-01 Sunday 9start 18end
2018-12-02 Sunday 10start 20end
In the above the sunday is being repeated. How can I get the correct days? e.g. Saturday and Sunday etc.
You are not calculating the day for each date. You are using the already calculated day.
$date = $_POST['schedule-date'];
// rest of the code
foreach( $date as $key => $n ) {
$day = date("l", strtotime($n)); // move this line inside for loop
echo $date[$key]." ".$day." "$start." ".$end;
}

Calculate time difference button

Hi I am new to javascript and php. I have a form that has three text boxes, starttime, endtime and results. I am trying to take the starttime and endtime and find how much time has passed and display this in the results. How can I go about this?
here is my html:
<form action="JobChangeTimeSheet.php" method="GET">
Machine Stopped at:
<input type="time" name="start_date"/>
End of TO:
<input type="time" name="since_start"/>
<input type="submit">
Total time
<?PHP
if (! empty($_GET['start_date'])){
$start_date = new DateTime($_GET['start_date']);
$since_start = $start_date->diff(new DateTime($_GET['since_start']));
echo $since_start->days.' days ';
echo $since_start->h.' hours';
echo $since_start->i.' minutes';
}
?>
</form>
You can see there are 2 text boxes, a button, and a third disabled text box. I am trying to view the difference in minutes between the two text boxes in the third text box
Since you do not have a code I can work on, I will guide you exactly through the steps on how to do this.
Load your HTML input in PHP vars named $start_date and $end_date. Use these lines:
$start_date = new date("Y-m-d H:i:s",strtotime($start_date));
$end_date = new date("Y-m-d H:i:s",strtotime($end_date));
$interval = $start_date->diff($end_date);
$hours = $interval->format('%h');
$minutes = $interval->format('%i');
echo 'Difference in minutes is: '.($hours * 60 + $minutes);
PS: You are talking about Javascript in your question but no Javascript code has been presented in your code. Are you using some sort of DateTimePicker?

php for loop for date increment each time

I want to loop a date so that every time date is increment by previous date. my code is here. plz reply anyone, thanks in advance
$today = date('Y-m-d');
for($i=1; $i<=4; $i++){
$repeat = strtotime("+2 day",strtotime($today));
echo $rdate = date('Y-m-d',$repeat);
}
I want result as if today is 2016-04-04 than, 2016-04-06, 2016-04-08, 2016-04-10, 2016-04-12.
actually i want to make a reminder date where user enter reminder. lets a user want to add reminder today and want repeat it 5 time after 2days, 3days or what ever he wants, in next comming day. than how i repeat date with for loop.
Try this:
<?php
$today = date('Y-m-d');
for($i=1; $i<=4; $i++)
{
$repeat = strtotime("+2 day",strtotime($today));
$today = date('Y-m-d',$repeat);
echo $today;
}
Output:
2016-04-06
2016-04-08
2016-04-10
2016-04-12
The easiest way is what answer
aslawin
The below example is to go through the date
$begin = new DateTime($check_in);
$end = new DateTime($check_out);
$step = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $step, $end);
foreach ($period as $dt)
{
<sample code here>
}
You can try this:
$today = date('Y-m-d');
for($i=1; $i<=8; $i++){
if($i%2 == 0){
$repeat = strtotime("+$i day",strtotime($today));
echo $rdate = date('Y-m-d',$repeat);
}
}
Result:
2016-04-06
2016-04-08
2016-04-10
2016-04-12
In this example, you can use $i%2 == 0 with limit <= 8
Use a for loop with base 2, then directly output your dates:
for( $i=2; $i<9; $i=$i+2 )
{
echo date('Y-m-d', strtotime( "+ $i days" )) . PHP_EOL;
}
Result:
2016-04-06
2016-04-08
2016-04-10
2016-04-12
actually i want to make a reminder date where user enter reminder.
lets a user want to add reminder today and want repeat it 5 time after
2days, 3days or what ever he wants, in next comming day. than how i
repeat date with for loop.
I'll help with the above. First of all I will just say I have a huge personal preference towards the DateTime object over simply using date it's more flexible and a hell of a lot more readable in my opinion, so when working with dates I would always suggest using that over date()
So here is some Code:
$date = new DateTime(); // Pretend this is what the User entered. We got it via $_POST or something.
$interval = 2; // Repeat x times at y day intervals. (Not including the initial)
$repeatAmount = 2; // Repeat the reminder x times
for ($i = 0; $i <= $repeatAmount; ++$i) {
echo $date->format('d/m/Y');
$date->modify('+'. $interval .' day');
}
$date = new DateTime()Imagine this is the date the user entered, this is our starting point, our first reminder will at this time.
$interval and $repeatAmount are the interval in days, i.e. I want this to every 2 days and the amount of times you want it to repeat, in our example 2.
for ($i = 0; $i <= $repeatAmount; ++$i) { We want to loop as many times as the user says they want to repeat. Little note ++$i tends to be a very minor performance boost over $i++ in some scenarios, so it is usually better to default to that unless you specifically need to use $i++
echo $date->format('d/m/Y'); Simply print out the date, i'll let you handle the reminder logic.
$date->modify('+' . $interval . ' day'); Increment the dateTime object by the interval that the user has asked for, in our case increment by 2 days.
Any questions let me know.

PHP date increment every x minutes and loop until x times

I'm new in php.
I want to ask how to make increment loop date every x minus or x hours and repeat until x times.
Example :
I have time : 2016-03-22T23:00:00
I want to increment that time every 30 minutes
And repet until 6 times.
and output will be :
2016-03-22T23:00:00
2016-03-22T23:30:00
2016-03-23T00:00:00
2016-03-23T00:30:00
2016-03-23T01:00:00
2016-03-23T01:30:00
My former code form other question in stackoverflow :
<?php
$startdate=strtotime("next Tuesday");
$enddate=strtotime("+1 weeks",$startdate); //16 weeks from the starting date
$currentdate=$startdate;
echo "<ol>";
while ($currentdate < $enddate): //loop through the dates
echo "<li>",date('Y-m-d', $currentdate),"T";
echo date('H:i:s', $currentdate);
echo "</li>";
$currentdate = strtotime("+30 minutes", $currentdate); //increment the current date
endwhile; // calculate date range
echo "<ol>";?>
On that code the loop stop until desire date.
My problem is how to make increment in time until desire times..., for example 5 , 10, 10, 500 times loop?
How to code that?
PHP's DateTime feature has the perfect option to do what you want:
// Set begin date
$begin = new DateTime('2016-03-17 23:00:00');
// Set end date
$end = new DateTime('2016-03-18 23:00:00');
// Set interval
$interval = new DateInterval('PT30M');
// Create daterange
$daterange = new DatePeriod($begin, $interval ,$end);
// Loop through range
foreach($daterange as $date){
// Output date and time
echo $date->format("Y-m-dTH:i:s") . "<br>";
}
Just use for-loop for this task:
<?php
$desiredtimes = 500; // desired times
$startdate=strtotime("next Tuesday");
$currentdate=$startdate;
echo "<ol>";
for ($i = 0; $i < $desiredtimes; $i++){ //loop desired times
echo "<li>",date('Y-m-d', $currentdate),"T";
echo date('H:i:s', $currentdate);
echo "</li>";
$currentdate = strtotime("+30 minutes", $currentdate); //increment the current date
} // calculate date range
echo "<ol>";?>
Use a DateTime object, then setup a loop that will iterate exactly 6 times.
After printing the current datetime, increase the datetime object by 30 minutes.
Code: (Demo)
$dt = new DateTime("2016-03-22T23:00:00");
for ($i = 0; $i < 6; ++$i, $dt->modify('+30 minutes')) {
echo $dt->format("Y-m-d\TH:i:s") . "\n";
}
Output:
2016-03-22T23:00:00
2016-03-22T23:30:00
2016-03-23T00:00:00
2016-03-23T00:30:00
2016-03-23T01:00:00
2016-03-23T01:30:00
This was inspired by a more complex process that I scripted for CodeReview.

How to compare digits with date PHP

Is there a way to convert values to date? I have 3 dropdown lists: day, month and year. If the selected date < today, then the consumers won't be able to click on the buy button til the day i selected in de dropdown.
The problem is, that the date I selected, is a value and not a date. Is there a way to compare my value with the date of today? The code below is the backend of my website, the if statement has to stay on the frontend
$releasedatumdag= get_post_meta( $domeinnaam_extensies->ID, 'releasedatumdag', true );
$releasedatummaand= get_post_meta( $domeinnaam_extensies->ID, 'releasedatummaand', true );
$releasedatumjaar= get_post_meta( $domeinnaam_extensies->ID, 'releasedatumjaar', true );
<tr>
<th>Releasedatum:</th>
<td>
<select name="domeinnaam_extensies_releasedatumdag">
<?php
for($idag = 1; $idag <= 31; $idag++){
echo '<option value="'.$idag.'">'.$idag.'</option>';
}
?>
</select>
<select name="domeinnaam_extensies_releasedatummaand">
<?php
for($imaand = 1; $imaand <= 12; $imaand++){
echo '<option value="'.$imaand.'">'.$imaand.'</option>';
}
?>
</select>
<select name="domeinnaam_extensies_releasedatumjaar">
<?php
for($ijaar = 2000; $ijaar < date("Y")+10; $ijaar++){
echo '<option value="'.$ijaar.'">'.$ijaar.'</option>';
}
?>
</select>
</td>
</tr>
You can use DateTime() to create your date. You then can compare it to another DateTime object representing today since DateTime objects are comparable:
$my_date = DateTime::createFromFormat('d/m/Y', '31/12/2014');
$now = new DateTime();
if ($my_date < $now) {
// do something
}
You have a day, month, and a year so just create a DateTime object and you can get the timestamp for it and compare it to today.
http://www.php.net/manual/en/datetime.construct.php
http://php.net/manual/en/function.checkdate.php
Can string the digits into any format that you want
$dateString = $releasedatumdag . '/' . $releasedatummaand . '/' . $releasedatumjaar;
//Make sure that they didn't try to create Feb. 31
if(checkdate($dateString)) {
$submittedTime = new DateTime($dateString);
$today = new DateTime();
//DO YOUR COMPARISON HERE
}

Categories