Getting NAN on safari when using a javascript date - javascript

I am working on something, in which I am required to subtract 2 dates from one another, the difference would then be multiplied by the price per night of the event.
The issue I am having is that, while this works perfectly when using Chrome or Firefox, I am getting "NAN if I am using safari.
Date to and from are obtained using the php code below:
<?php
// displays days with leading 0s
$options = array();
for ($i=1; $i<32; $i++) {
$theday = date('d', mktime(0,0,0,0,$i,2000));
$sel = ($i == date('d') ? 'selected="selected"' : '');
$options[] = "<option value= \"{$theday}\" {$sel}>{$theday}
</option>";
}
$options_list = join("\r\n", $options);
echo "<div class='select' id='date'><select class=\"short-input day-to\" name=\"day_to\">{$options_list}</select></div>";
?>
<?php
$options = array();
for ($i = 1; $i<13; $i++) {
$themonth = date('F', mktime(0,0,0,$i,2, 2000));
$month = date('m', mktime(0,0,0,$i,2,2000));
$sel =($i == date('n') ? ' selected="selected"' : '');
$options[] = "<option value=\"{$month}\" {$sel}>{$themonth}
</option>";
}
$options_list = join("\r\n", $options);
echo "<div class='select' id='month'><select class=\"short-input month-to\" name=\"month_to\" size=\"1\">{$options_list}</select></div>";
?>
<?php
/* build selection list for the year */
$today = time(); // stores today's date
$startYr = date("Y", $today); // get the year from $today
echo "<div class='select' id='year'>
<select class=\"short-input year-to\" name='year_to'>\n";
for ($year=$startYr;$year<=$startYr+10;$year++)
{
echo " <option value= $year";
if ($startYr == $year)
{
echo "";
}
echo " > $year</option>\n";
}
echo "</select>\n</div>\n";
?>
http://jsfiddle.net/ju097j4z/#
Are there any solutions to this.

instead of Date.parse(monthFrom + ' ' + dayFrom + ' ' + yearFrom); in your fiddle use new Date(yearFrom, monthFrom, dayFrom); *note that in js month starts with 0

Related

Change color of row based on time [duplicate]

echo '<table style="width:100%"> <tr>';
echo '<td>Order</td>';
echo '<td>Destination</td>';
echo '<td>Location</td>';
echo '<td>Status</td>';
echo '<td>TimeStamp</td>';
echo '</tr>';
if($result) {
while($row = mysqli_fetch_assoc($result)) {
echo '<tr><td>';
echo $row['OrderNumber'] . '';
echo '</td><td>';
echo $row['Destination'] . '';
echo '</td><td>';
echo $row['Location'] . '';
echo '</td><td>';
echo $row['Status'] . '';
echo '</td><td>';
echo $row['TimeStamp'] . '';
echo '</td></tr>';
}
echo '</table>';
}
I want to change the background of the row turned a different color is the time stamp is more than 60 minutes past the current time. any help would be much appreciated. i dont even know where to begin.
Thanks
edit: format of my time stamp "2015-07-17 19:17:31"
Do an if to see if time is over 60 minutes and if so assign it a class with a different background color. Since you didn't clarify I'm going to assume you are using unix timestamp time().
$currTime = time();
if($result) {
while($row = mysqli_fetch_assoc($result)) {
$calc = $currTime - $row['TimeStamp'];
if($calc > 3600){
$rowClass = "oldOrder";
} else {
$rowClass = "normalOrder";
}
echo '<tr class="'.$rowClass.'"><td>';
echo $row['OrderNumber'] . '';
echo '</td><td>';
echo $row['Destination'] . '';
echo '</td><td>';
echo $row['Location'] . '';
echo '</td><td>';
echo $row['Status'] . '';
echo '</td><td>';
echo $row['TimeStamp'] . '';
echo '</td></tr>';
}
Then add CSS to define the two classes
.oldOrder{
background-color: #ccc;
}
.normalOrder{
background-color: #fff;
}
$NowDT = date("Y-m-d H:i:s");
$NowRDT = strtotime($NowDT);
$DateTimeNF = $row['TimeStamp'];
$TimeRF = strtotime($DateTimeNF);
$TimeDifference = $NowRDT - $TimeRF;
$TimeDifferenceHours = $TimeDifference/3600;
If ($TimeDifferenceHours > "1")
{
echo '<tr bgcolor="#E60000"><td>';
}
else
{
echo '<tr><td>';
}
It seems like your timestamp is a String, you need to convert your string to a Unix TimeStamp using strtotime:
$rowTime=strtotime($row['TimeStamp']);
and then substract it from the actual time, as the previous answer, to obtain the difference in seconnds. Divide it by 60 and you get it on minutes.
$currenTime=time();
$diffSeconds=$currenTime-$rowTime;
$diffMinutes=$diffSeconds/60;
and then check if the difference is bigger than 60:
$myCSS="normalRow";
if ($diffMinutes>60) { $myCSS="differentRow"; }
and then use that CSS Style ($myCSS) in your table row:
echo '<tr class="'.$myCSS.'"><td>';
You need to define those two styles in your CSS file or your HTML Header like:
<style>
.normalRow {
background-color: #888;
}
.differentRow {
background-color: #ccc;
}
</style>
And that's it. Hope it helps.

cannot get id of li on click event

I have this code to show tabs.
$start = strtotime($_GET['date']);
$dates = array();
for ($i = 0; $i <= 7; $i++) {
$date = date('Y-m-d', strtotime("+$i day", $start));
$date1 = $date;
$day = date('D', strtotime($date1));
$date = explode('-', $date);
$dateinput = date('Y-m-d', strtotime("+$i day", $start));
$date = $date[2];
echo '<li class="lia" id="'.$dateinput.'"><input type="hidden" class="getdate" value="'.$dateinput.'">' . $date . ' ' . $day . '</li>';
}
I have a date-picker and i want to get the id of li by on change function of datepicker. I am using this code at moment.
$("#calendar-<?php echo $post->ID?>").on("change",function(){
var date = $(this).val();
console.log(date);
var libtn= $("li.lia").find("li").attr("id");
console.log(libtn);
alert(libtn);
});
It is showing undefined in alert box. Where i am doing wrong?
Replace
var libtn= $("li.lia").find("li").attr("id");
With
var libtn = $("li.lia").attr("id");
And check.
You can not find element itself inside same element.
EDIT
I am expecting you will have lia class everywhere means on every li.
Then,
Here is the code,
$("li.lia").each(function(i,j){
console.log($(this).attr("id")); // use as per your requirement
});

calculate and display difference between 2 dates before submitting form

I would like to find find a way to calculate the difference between 2 dates, and display the result before I submit the form. This will be similar to the check in - check out drop downs in the yellow/ orange box in http://www.booking.com/ where on selecting the dates the number of nights is immediately displayed.
HTML version of code as displayed in JS Fiddle: http://jsfiddle.net/chri_chri/xy2gpa02/2/
Code used to generate the drop down date selectors
<?php
// displays days with leading 0s
$options = array();
for ($i=1; $i<32; $i++) {
$theday = date('d', mktime(0,0,0,0,$i,2000));
$sel = ($i == date('d') ? 'selected="selected"' : '');
$options[] = "<option value= \"{$theday}\" {$sel}>{$theday}
</option>";
}
$options_list = join("\r\n", $options);
echo "<div class='select' id='date'><select class=\"short-input\" name=\"day_from\">{$options_list}</select></div>";
?>
<?php
$options = array();
for ($i = 1; $i<13; $i++) {
$themonth = date('F', mktime(0,0,0,$i,2, 2000));
$month = date('m', mktime(0,0,0,$i,2,2000));
$sel =($i == date('n') ? ' selected="selected"' : '');
$options[] = "<option value=\"{$month}\" {$sel}>{$themonth}
</option>";
}
$options_list = join("\r\n", $options);
echo "<div class='select' id='month'><select class=\"short-input\" name=\"month_from\" size=\"1\">{$options_list}</select></div>";
?>
<?php
/* build selection list for the year */
$today = time(); // stores today's date
$startYr = date("Y", $today); // get the year from $today
echo "<div class='select' id='year'>
<select class='short-input' name='year_from'>\n";
for ($year=$startYr;$year<=$startYr+10;$year++)
{
echo " <option value= $year";
if ($startYr == $year)
{
echo "";
}
echo " > $year</option>\n";
}
echo "</select>\n</div>\n";
?>
you have two options, use java-script or submit form to php file and process the form.
jsfiddle working example http://jsfiddle.net/mdamia/3b5fyzjL/32/
// the script
function stayCost() {
var dayFrom = $('.day-from').val();
var monthFrom = $('.month-from').val();
var yearFrom = $('.year-from').val();
var dateFrom = Date.parse(monthFrom + ' ' + dayFrom + ' ' + yearFrom);
var dayTo = $('.day-to').val();
var monthTo = $('.month-to').val();
var yearTo = $('.year-to').val();
var dateTo = Date.parse(monthTo + ' ' + dayTo + ' ' + yearTo);
var dateDifference = dateTo - dateFrom;
var diffInDays = Math.ceil(dateDifference / (1000 * 3600 * 24));
$('.days').text(diffInDays);
$('.cost').text(diffInDays * 40);
}
stayCost();
$('.day-to').change(function () {
stayCost();
});

Send and set 3 selected values from Dropdown menu to JavaScript function PHP

I have three drop down menus:
<?php
if(isset($som)) {
=======first drop down=======
echo "<select id="."som-patch1"." class="."form-control".">";
echo "<option value="."select".">Please Select SOM patch...</option>";
for ($i=0; $i < count($som) ; $i++) {
echo '<option value="'.$som[$i]['som'].'">'.$som[$i]['som'].' </option>';
}
echo "</select>";
=======second drop down=======
echo "<select id="."som-patch1-month"." class="."form-control".">";
echo "<option value="."select".">Please Select Month ...</option>";
for ($i=0; $i < 12 ; $i++) {
$month = date('F', strtotime('-'.$i.' month', strtotime(date('Y-m-01'))));
echo '<option value="'.$month.'">'.$month.'</option>';
}
echo "</select>";
=======third drop down=======
echo "<select id="."som-patch1-year"." class="."form-control".">";
echo "<option value="."select".">Please Select Year ...</option>";
for ($i=0; $i < 12 ; $i++) {
$year = date('Y', strtotime('-'.$i.' year', strtotime(date('Y-m-01'))));
echo '<option value="'.$year.'">'.$year.'</option>';
}
echo "</select>";
}
?>
I can successfully send the selected values to the following JavaScript function:
$('[id^=som-patch1]').on('change', function() {
alert( this.value );
});
The problem I have is:
I am unable to set the relevant values in the variables so that I could call the add_som_donut_chart_org_data() method.
Also I need to make sure all THREE values have been selected before the function call.
I have tried the following:
$('[id^=som-patch1]').on('change', function() {
var som = null;
var month = null;
var year = null;
if (id=='som-patch1'){
som = this.value;
}
alert( som );
});
But this doesn't set the som value.
The desired outcome should be:
if (som !== null && month !== null && year !== null) {add_som_donut_chart_org_data(som,month,year)}
Thanks in advance.
UPDATE: The following code resolved the issue
$('[id^=som-patch1]').on('change', function() {
var id = $(this).attr("id");
if (id == 'som-patch1') {
som = this.value;
}
if (id == 'som-patch1-month') {
month = this.value;
}
if (id == 'som-patch1-year') {
year = this.value;
}
if (som !== null && month !== null && year !== null) {
add_som_donut_chart_org_data(som,month,year);
}
});
As per your current code condition if (id=='som-patch1') would never be true, because id is not defined yet. Hence it will not set som variable. The reason is very basic that you have not defined id variable in your code. Before your condition use following code:
var id = $(this).attr("id");

I created a dropdown list and how to display the other columns data of selected item?

this is my code for dropdown list
<?php
$sql = "SELECT * FROM parts";
$result = mysql_query($sql);
echo "<select name='name' class='ed'>";
echo "<option id='0'> --Select Part Name-- </option>";
while ($row = mysql_fetch_array($result)) {
$op1=$row['part_description'];
$op2=$op1.$row['price'];
$op3=$op2.$row['weight'];
echo "<option value='" . $row['part_description'] ."'>" .$op3 ."</option>";
echo $op3;
}
echo "</select>";
?>
I have 3 columns for my database
part_description, price and weight. In the dropdownlist i created, you can select stored items at part_description. What I want to do is if I select that item in drop down list it will also display also the price and weight. But the display should be out of the box.
Please help me thanks
<?php
$sql = "SELECT part_description FROM parts";
$result = mysql_query($sql);
echo "<select name='name' class='ed'>";
echo "<option id='0'> --Select Part Name--</option>";
while ($row = mysql_fetch_array($result)) {
$op1=$row['part_description'];
$op2=$op1.$row['price'];
$op3=$op2.$row['weight'];
echo "<option value='" . $row['part_description'] ."'>" .$op3 ."</option>";
}
echo "</select>";
?>
This is for print three value in single option tag.
And let me know Is this your expectation.
You can do it in Javascript.
<?php
$parts = mysqli_query($conn, "SELECT part_description, price, weight FROM parts");
$theParts = [];
while ($row = mysqli_fetch_assoc($parts)) {
$part = [];
$part['description'] = $row['part_description'];
$part['price'] = $row['price'];
$part['weight'] = $row['weight'];
$theParts[] = $part;
}
?>
<script type="text/javascript">
var theParts = <?php echo(json_encode($theParts));?>;
function createDropDown() {
var theDropdown = "<select name='name' id='theDropDown' class='ed' onchange='didChange()'>";
theDropdown += "<option value='0'>--Select Part Name--</option>";
theParts.forEach(function(entry) {
theDropdown += "<option value='" + entry['description'] + "'>" + entry['description'] + "</option>";
});
theDropdown += "</select>";
return theDropdown;
}
function didChange() {
var e = document.getElementById("theDropDown");
var str = e.options[e.selectedIndex].value;
var price = null;
var weight = null;
theParts.forEach(function(entry) {
if (entry['description']==str) {
price = entry['price'];
weight = entry['weight'];
}
});
}
</script>
(I typed this code directly into StackOverflow and haven't tested it but it should work)
EDIT:
Here's how to do it using MySQL, although I don't recommend using it.
$conn = mysql_connect('localhost', 'username', 'password');
mysql_select_db('database', $conn);
$results = mysql_query("SELECT part_description, price, weight FROM parts");
while ($row = mysql_fetch_assoc($results)) {
//Same as above.
}

Categories