create an notification/alert at an specific time - php

I have created a timer to be used as a reference on a alert when the timer reaches exactly 5:00 pm/ 17:00. Do you have any ideas how could I do that? Do I check the time once in a while?
Here's how I create my timer.
<script language="JavaScript">
var tick;
function stop() {
clearTimeout(tick);
}
function usnotime() {
var datetoday = document.getElementById("datenow")
var currentDate = new Date()
var day = currentDate.getDate()
var month = currentDate.getMonth() + 1
var year = currentDate.getFullYear()
datetoday.val("" + month + "/" + day + "/" + year + "");
var txt = document.getElementById("timeticker")
var ut = new Date();
var h, m, s;
var time = " ";
h = ut.getHours();
m = ut.getMinutes();
s = ut.getSeconds();
if (s <= 9) s = "0" + s;
if (m <= 9) m = "0" + m;
if (h <= 9) h = "0" + h;
time += h + ":" + m + ":" + s;
txt.innerHTML = time;
tick = setTimeout("usnotime()", 1000);
}
//--> document.rclock.rtime.value=time; tick=setTimeout("usnotime()",1000); } //-->
</script>
<tr>
<td class="tdlabel">Current Time:</td>
<td>
<div id="timeticker" name="rtime" size="22"></div>
</td>
</tr>
And it seems that my timer is not displaying. Any ideas??

You have a mistake, you mixed javascript and jquery.
datetoday.val("" + month + "/" + day + "/" + year + "");
Should be (assuming datetoday is a textfield. If div use innerHTML instead of value)
datetoday.value="" + month + "/" + day + "/" + year + "";
and add just before the closing script tag
window.onload=function(){
usnotime();
}

Not sure if this is the exact answer; it might help posting a bit more code.
I don't see how you're starting the method. You might need to add a button-execute / page-load call of usnotime().
Do you have real objects for all of those variables? (I.e. timeticker, datenow).
Check your JavaScript error log in the browser. It's a bit hard without more code for me to see what's wrong, so either include more, or use the debugger.

Using the setTimeout() u can do , Refer this it may be useful for you

This may help you....
<html>
<head>
<title>(Type a title for your page here)</title>
<script type="text/javascript">
function display_c(){
var refresh=1000; // Refresh rate in milli seconds
mytime=setTimeout('display_ct()',refresh)
}
function display_ct() {
var strcount
var x
var x1
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
var seconds= currentTime.getSeconds()
x=month + "/" + day + "/" + year
if (minutes < 10){
minutes = "0" + minutes
}
x=x+" "+hours + ":" + minutes +":"+seconds+ " "
x1=hours + ":" + minutes + ":"+seconds+" "
if(hours > 11){
x=x+"PM"
x1=x1+"PM"
if(x1=="5:30:0 PM"){alert(x1);}
} else {
x=x+"AM"
x1=x1+"AM"
if(x1=="8:16:0 AM"){alert(x1);}
}
document.getElementById('ct').innerHTML = x;
tt=display_c();
}
</script>
</head>
<body onload=display_ct();>
<span id='ct' ></span>
</body>
</html>

Related

CodeIgniter Date or DateTime

I have one problem, which I can not resolve and have no idea where is mistake
One I create a tabel called posts and in codeigniter create a form Posts, after createing all post I want to display posts in index.php and I have table called
Date of departure, and date of return. One i create post date looking good but when I display in my index.php it's look like 0000-00-00 00:00:00
In begginig data type was Date, now i change to be DateTime but nothing happend, same problem
Maybe I make mistake in my JS script
Any comment ?
My script
<script>
$( function() {
$( "#datepicker" ).datepicker();
$( "#datepicker1" ).datepicker();
} );
</script>
When your field in MySQL is Date-time it aspects proper format before saving in database.
So convert string to date time format in php using.
date("Y-m-d H:i:s", strtotime($string));
Or you can do the same in Datepicker
$(function() {
$('#datepicker').datepicker({
dateFormat: 'yy-dd-mm',
onSelect: function(datetext){
var d = new Date(); // for now
var h = d.getHours();
h = (h < 10) ? ("0" + h) : h ;
var m = d.getMinutes();
m = (m < 10) ? ("0" + m) : m ;
var s = d.getSeconds();
s = (s < 10) ? ("0" + s) : s ;
datetext = datetext + " " + h + ":" + m + ":" + s;
$('#datepicker').val(datetext);
},
});
});
Fiddle for datepicker
Note if you want date only in MySQL field the omit hour min sec part of conversion

jQuery datepicker disabled dates - multiple ranges

im a javascript / jQuery n00b so bear with me
I have a table in the database that contains multiple rows with startdate & enddate columns
I have on the page a datepicker jquery element
What I need it to do is on the page load check the database table and all the dates from startdate to enddate to be disabled
I have the below which would do individual dates however I dont know how to get it to loop and disable multiple entries ( ranges - from date to date )
For example 24-4-2017 to 26-4-2017 disabled also 30-4-2017 to 17-5-2017 disabled
so multiple ranges need to be disabled
Code I have so far below, please help guys Ive been racking my brains over this for 2 days now and have severe code block
var disabledDays = ["22-04-2017"]; // M-DD-YYYY Format
/* utility functions */
function nationalDays(date) {
var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();
//console.log('Checking (raw): ' + m + '-' + d + '-' + y);
for (i = 0; i < disabledDays.length; i++) {
if($.inArray((m+1) + '-' + d + '-' + y,disabledDays) != -1 || new Date() > date) {
//console.log('bad: ' + (m+1) + '-' + d + '-' + y + ' / ' + disabledDays[i]);
return [false];
}
}
//console.log('good: ' + (m+1) + '-' + d + '-' + y);
return [true];
}
/*
Above 2 are probably redundant because BaT are open Bank Holidays
and also open weekends so ..
*/
/*
Create DatePicker
Change MaxDate below to show more months
*/
jQuery(document).ready(function() {
jQuery('#date').datepicker({
minDate: new Date(2017, 0, 1),
maxDate: new Date(2017, 6, 31),
dateFormat: 'DD, MM, d, yy',
constrainInput: true,
beforeShowDay: nationaldays
});
});
Just compare the day that is currently rendering with disabled dates range.
$(document).ready(function() {
var startDisabledDates = new Date(2017, 03, 10),
endDisabledDates = new Date(2017, 03, 20);
$("#date").datepicker({
beforeShowDay: function(day) {
var isSelectable = day < startDisabledDates || day > endDisabledDates;
return [isSelectable];
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<input type="text" id="date">

Insert current timestamp using javascript in database using php

I am trying to insert local time into database using javascript. Can any one help me?
could you please have a look on the file named member.class.php and please let me know where I have to put the javascript code... https://www.dropbox.com/sh/lfef67brewx7rub/wYRP72bDh7
Its very simple man...try with this
var milliseconds = new Date().getTime();
it will give you time in milli seconds or you can use like
new Date().getTime();
if you want it in unix(linux) timestamp then use like this
var unix = Math.round(+new Date()/1000); it will also give you in seconds
var currentdate = new Date();
var datetime = "Date n Tym : " + currentdate.getDate() + "/"
+ (currentdate.getMonth()+1) + "/"
+ currentdate.getFullYear() + " # "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
10 ways to format time and date using JavaScript
See this answer!
In Javascript:
var d = new Date();
var date_with_time = d.getDate()+"-"
+d.getMonth()+1+"-"
+d.getFullYear()+" "
+d.getHours()+" "
+d.getMinutes()+" "
+d.getSeconds();
In PHP, the best way will be that you should create your time column DATETIME type and set default value CURRENT_TIMESTAMP. No need to manually insert it through Javascript.
DB like time stamp in js:
var timestamp = "" + d.getFullYear()
+ ("0" + (d.getMonth()+1)).slice(-2)
+ ("0" + d.getDate()).slice(-2) + "-"
+ ("0" + d.getHours()).slice(-2)
+ ("0" + d.getMinutes()).slice(-2)
+ ("0" + d.getSeconds()).slice(-2);
after this, for example, timestamp="20160408-134304"

jQuery and PHP Countdown timer from formatted variable

I need to build a countdown timer in jquery that replaces the html in a span tag from a php variable that's formatted to XX Days XX:XX:XX. It should load the variable on page load and then when the jquery script loads it starts the countdown but keeping the same format (XX Days XX:XX:XX) and eventually displays "Expired" and stops the countdown.
I have a jsFiddle started (http://jsfiddle.net/2SDdh/1/) but it shows the formatted time and then loads a 10 sec countdown and eventually shows "Expired". Anyone know how to get the formatting correct?
My HTML output via PHP
<span class="exp">10 Days 10:10:10</span>
My jQuery
$(function(){
var count = 10;
countdown = setInterval(function(){
if (count > 0) {
count--;
$(".exp").html(count);
} else {
$(".exp").html('Expired');
}
}, 1000);
});
try this
html:
<span class="days">10</span> Days <br />
<span class="exp">23:59:59</span>​
javascript :
$(function(){
var days = 10;
var count = 86399;
var count2 = 0;
var hour = 0;
var min = 0;
var sec = 0;
countdown = setInterval(function() {
if (days != 0 || count != 0) {
if (count == 0) {
days--;
count = 86399;
} else {
count--;
}
count2 = count;
hour = Math.floor(count2 / 3600);
count2 = count2 % 3600;
min = Math.floor(count2 / 60);
count2 = count2 % 60;
sec = count2;
$(".exp").html(hour + ":" + min + ":" + sec);
$(".days").html(days);
} else {
$(".exp").html('Expired');
}
}, 1000);
});​
Cheers!
Instead of rolling your own countdown timer, why not use one that already exists?. KK Countdown works well for me.

Javascript .getHours() returns strange result

I'm converting a PHP timestamp into a human readable form in javascript:
<script type="text/javascript">
var timeleft = new Date( <?php echo $lefttime; ?> * 1000 );
var hours = timeleft.getHours();
var minutes = timeleft.getMinutes();
var seconds = timeleft.getSeconds();
var countdown = function() {
console.log( "You have " + hours + ":" + minutes + ":" + seconds + " seconds left" );
</script>
The minutes and seconds work but I'm getting strange results for the hours. For example:
Timestamp: 1976
Function returns: 19:32:56
Any ideas why I'm getting "19" from .getHours() ?
I'm going to hazard a guess that you're in Eastern Time. What you are seeing is $lefttime seconds after the epoch, which in the case of EST is 7PM.
To fix it, just use basic math:
var timeleft = <?php echo $lefttime; ?>;
var hours = Math.floor(timeleft/3600);
var minutes = Math.floor(timeleft/60)%60;
var seconds = timeleft%60;

Categories