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?
Related
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
I Have a php script
if (isset($_POST['getdays'])) {
$birthday = $_POST['daysalive'];
$now = date("Y-m-d");
$days_alive = date_diff(date_create($birthday), date_create($now));
}
Which returns me the amount of days a user lives by echoing:
$days_alive->format('%a days old');
<input name="daysalive" type="text" class="mt-3" placeholder="12-12-2018">
<button name="getdays" class="btn btn-primary mt-3" type="submit">Check in what year you will be 20000 days old</button>
What I would like to do is take this input and determine in what year the user would be 20000 days old. I was thinking of looping through $days_alive using a for() loop with an if statement like so:
if($days_alive + 365 >= 20000) {
echo(date('Y');
}
any thoughts?
PHP has a date_add function which allows you to add a DateInterval to a DateTime object. So you just need to add a DateInterval of 20000 days like so:
date_add(date_create($birthday), new DateInterval('P20000D'));
There is no need for loops. You can use strtotime or DateTime to calculate it.
$birthday = "1975-01-01";
$res = strtotime($birthday . " + 20000 days");
echo date("Y-m-d", $res); //2029-10-04
please I need help with counter (timer) that use table data as minutes to count. For example, i table i have row that sets number 5 (it will always be in minutes) and on php page i have table with some rows. i need to create on every row Start/End button. Clinking on Start will user value from table and start counting 5:00, 4:59 etc. Problem is that timer should not reset after refresh page (many people will look at this page).
I have this script that calculates start time and end time, but it will reset after refresh.
I did not post while table with rows, I did not think it is necessary.
in table, output of column that contain minutes is
<?php echo $row['Trajanje_pauze'] ; ?>
this is my script, i found on this link
<?php
session_start();
?>
<form action="msa_pauze.php" method="POST">
<td> <input type="submit" name="start" value="Start"></td>
<br><br>
<td> <input type="submit" name="end" value="End"></td>
</form>
<?php
if(isset($_POST['start'])) {
date_default_timezone_set('Europe/Zagreb');
$time_start = date('Y-m-d H:i:s');
$_SESSION['start'] = $time_start;
echo "time start now $time_start <br>";
}
if(isset($_POST['end'])) {
date_default_timezone_set('Europe/Zagreb');
$time_end = date('Y-m-d H:i:s');
$_SESSION['end'] = $time_end;
echo "time was ended $time_end <br>";
$sst= new DateTime($_SESSION['start']);
$eet= new DateTime($_SESSION['end']);
$diff = date_diff($eet, $sst);
$timeelapsed = $diff->format('%r %a days, %h hours, %i minutes and %S seconds');
echo $timeelapsed;
}
?>
I'm looking to create a countdown timer in PHP. When a user clicks a button it saves the current date & time into a database entry, then it should take the difference of that entry with the current date and time and 'doSomething' when the difference is larger than 48 hours.
My issue is with the actual countdown.
I've tried the following but to no avail it only counts the difference of the of both strings and doesn't take the days in account. Not only that but it also appears to show the resulted difference incorrectly:
$d1=strtotime("2012-07-08 11:14:15");
$d2=strtotime("2012-07-09 12:14:15");
$diff = round(abs($d1 - $d2));
$cd = date("H:i:s", $diff);
echo $cd;
Thanks for helping me Yan.kun from StackOverflow! The code submitted below was the solution! In order to display strictly the countdown in hours:minutes:seconds I replaced the printf() code with the following:
$hours = ($result->d*24)+$result->h;
$minutes = $result->i;
$seconds = $result->s;
echo $hours . ":" . $minutes . ":" . $seconds;
Try this instead:
$d1 = new DateTime("2012-07-08 11:14:15");
$d2 = new DateTime("2012-07-09 12:14:15");
$result = $d1->diff($d2);
printf('difference is %d day(s), %d hour(s), %d minute(s)', $result->d, $result->h, $result->i);
EDIT:
And if you have no PHP 5.3 available, you can convert your times into an unix epoch timestamp like described in this answer.
not sure if this is what you want:
$d1=strtotime("2012-11-18 11:14:15");//2 days earlier
$d2=strtotime("2012-11-20 11:14:15");//today
$diff = $d2 - $d1; //difference in seconds.
$hours = $diff/60/60;//translation to minutes then to hours
echo $hours;
if($hours>48){
echo "Script";
}
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Php.Advance weekly calendar one week
I have written a script that displays a calendar week by week that the user can chose to go back or forward a week at a time. Everything works great except the first week of every year still displays the wrong year and the 31st December shows as 02/01. It seems that only week 1 is affected, the days are correct again in week and onwards
<?
if(isset($_POST['add_week'])){
$last_week_ts = strtotime($_POST['last_week']);
$display_week_ts = $last_week_ts + (3600 * 24 * 7);
} else if (isset($_POST['back_week'])) {
$last_week_ts = strtotime($_POST['last_week']);
$display_week_ts = $last_week_ts - (3600 * 24 * 7);
} else {
$display_week_ts = floor(time() / (3600 * 24)) * 3600 * 24;
}
$week_start = date('d-m-Y', $display_week_ts);
$week_number = date("W", $display_week_ts);
$year = date("Y", $display_week_ts);
echo $week_start.' '.$week_number.' '.$year;
?>
<table name="week">
<tr>
<?
for($day=1; $day<=7; $day++)
{
echo '<td>';
echo date('d-m-Y', strtotime($year."W".$week_number.$day))." | \n";
echo '</td>';
}
?>
</tr>
<tr>
<form name="move_weeks" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="last_week" value="<? echo $week_start; ?>" />
<td colspan="7"><input type="submit" name="back_week" value="back_week" /><input type="submit" name="add_week" value="add_week" />
</td>
</form>
</tr>
</table>
Any help will be appreciated
As suggested in the answer to the original question, you need to not use the week number and year number stuff.
If at the end of the day, you want to be able to list seven days for the week, just do not use this section of code at all:
$week_number = date("W", $display_week_ts);
$year = date("Y", $display_week_ts);
echo $week_start.' '.$week_number.' '.$year;
And replace the loop that echos out with the loop suggested in the original question.
This has to do with the format you are providing date(). Specifically W.
From the PHP Docs:
ISO-8601 week number of year, weeks starting on Monday
It is coincidence this is appearing because December 31, 2012 is a Monday.
You should rewrite your code to use other ways to calculate dates. I'd recommend against trusting form data.