Using this I can get the date and time difference.
$first_date = new DateTime(“2012-11-30 17:03:30”);
$second_date = new DateTime(“2012-12-21 00:00:00”);
$difference = $first_date->diff($second_date);
echo format_interval($difference);
How can i convert it from static to run as count-down timer?
Try this jquery plugin
<script type="text/javascript" src="/path/to/jquery.countdown.js"></script>
<script type="text/javascript">
$(function() {
$('.yourCountdownContainer').countdown({
date: "June 7, 2087 15:03:26"
});
});
</script>
Form more details enter link description here
Related
I use the popular JQuery UI Datepicker to add/modify events on my web site. I am having a problem with getting the date to show up right using setDate. The incorrect date seems to always show up no matter what I do. Here are some various things I have tried:
The date shows up as 01/17/1970 when it should be 10/15/2015, this is how I need it to work, with the php:
$(function() {
var date = new Date(<?php echo strtotime($date); ?>);
$("#date").datepicker();
$("#date").datepicker("setDate", date);
$("#date").datepicker( "option", "showAnim", "slideDown" );
});
The date has inconsistent values when done like this, the date will always be wrong no matter what date you set, but there is no pattern.
$(function() {
var date = new Date(2015, 10, 15);
$("#date").datepicker();
$("#date").datepicker("setDate", date);
$("#date").datepicker( "option", "showAnim", "slideDown" );
});
The Javascript Date object's constructor takes milliseconds when given only one argument.
The PHP function strtotime() returns the Unix Timestamp, the number of seconds since 1970-01-01.
If you multiply the timestamp by 1,000, you'll get a suitable value to pass to the Date constructor.
var date = new Date(<?php echo (strtotime($date) * 1000); ?>);
I had also tried it like this previously, but forgot to put quotation marks around the php. Now it works:
<script>
$(function() {
$("#date").datepicker();
$("#date").datepicker("setDate", "<?php echo $date ?>");
$("#date").datepicker( "option", "showAnim", "slideDown" );
});
</script>
my jquery datepicker shows date correctly but the time isn't working. the test result produces the following: 1372046400 = 2013-06-24 04:06:00
as you can see the time isnt same as the systemtime, it should be 8:19pm.
<script type="text/javascript">
$(document).ready(function(){
$("#date").datepicker({
showButtonPanel: true,
minDate: '0M',
maxDate: '+90D',
dateFormat: "d-MM-yy",
onSelect : function(dateText, inst)
{ var epoch = $.datepicker.formatDate('#', $(this).datepicker('getDate')) / 1000;
$('#hidden1').val(epoch);
}
});
});
</script>
php script that displays the output
$dateTime = $_POST['hidden1'];
$date = new DateTime('#'.$dateTime);
echo $saveDate = $date->format('U = Y-m-d h:m:s');
http://php.net/manual/en/function.date.php
it has to be
$date->format('U = "Y-m-d H:i:s');
m is for month
set your time zone (http://php.net/manual/en/function.date-default-timezone-set.php) and then try to use the answer that Sanath has suggested.
Example:-
date_default_timezone_set('America/Los_Angeles');
I have a php file which I will later change into the form of an HTML file, which be current constraints are:
- Date format using Javascript.
Her script like the following snippet:
<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="js/script.js"></script>
<?php
/* Set start and end dates here */
$startDate = strtotime("15 August 2012 12:00:00");
$endDate = strtotime("15 November 2012 12:00:00");
/* /Set start and end dates here */
?>
<script type="text/javascript">
$(document).ready(function(){
JBCountDown({
secondsColor : "#ffdc50",
minutesColor : "#9cdb7d",
hoursColor : "#378cff",
daysColor : "#ff6565",
startDate : "<?php echo $startDate; ?>",
endDate : "<?php echo $endDate; ?>",
now : "<?php echo strtotime('now'); ?>",
seconds : "<?php echo date("s"); ?>"
});
});
</script>
please help me to overcome it.
I want to run a php script in javascript that is being subordinated.
example:
<script type="text/javascript">
$(document).ready(function(){
JBCountDown({
secondsColor : "#ffdc50",
minutesColor : "#9cdb7d",
hoursColor : "#378cff",
daysColor : "#ff6565",
startDate : startDate, //format time in JS
endDate : endDate, //format time in JS
now : strtotime('now'), //format time in JS
seconds : date("s")
});
});
</script>
Please help,
Regards
this can all be done in javascript.
<script type="text/javascript">
$(document).ready(function(){
//http://www.convert-unix-time.com/
var unix = Math.round(+new Date()/1000); //unix timestamp for todays date
//alert(unix);
JBCountDown({
secondsColor : "#ffdc50",
secondsGlow : "none",
minutesColor : "#9cdb7d",
minutesGlow : "none",
hoursColor : "#378cff",
hoursGlow : "none",
daysColor : "#ff6565",
daysGlow : "none",
startDate : "1362096000 ", //unix timestamp for ' Friday 1st March 2013 12:00:00 AM'
endDate : "1373328000", //unix timestamp for 'Tuesday 9th July 2013 01:00:00 AM'
now : unix,
seconds : unix % 60 //unix timestamp for seconds in realtime
});
});
</script>
Use strtotime() and date():
$originalDate = "2010-03-21";
$newDate = date("d-m-Y", strtotime($originalDate));
(see strtotime and date docs on the PHP site).
<?php
/* Set start and end dates here */
$startDate = new DateTime("15 August 2012 12:00:00");
$endDate = new DateTime("15 November 2012 12:00:00");
/* /Set start and end dates here */
print $startDate->format('FORMAT');
?>
See all date time formats: http://www.php.net/manual/en/datetime.formats.php
basically I know more or less how to do it but would like to know if there is any better way?
I have the following variables in PHP
$day; $month; $year;
The ones above have values from exploding a php date string.
Below is PHP plugin function which states the date for countdown.
<script type="text/javascript">
$(function () {
var austDay = new Date();
austDay = new Date(2013, 12, 22);
$('#defaultCountdown').countdown({until: austDay});
});
</script>
I would like to pass the date day/month/year variables into that function from PHP
how can I do it, when I tried to attach to the javavariable and put that variable in place of the date part, it didnt work.
Thanks for all help
var day = <?php echo $day ?>;
var month = <?php echo $month ?>;
var year = <?php echo $year ?>;
$(function () {
var austDay = new Date();
austDay = new Date( year, month, day );
$('#defaultCountdown').countdown({until: austDay});
});
There's a couple of ways you could skin this.
Fetch the values via $.ajax with php returning the values as a jsonified array (echo json_encode($my_values))
If the page generating the html is a php page then just new Date();
Place the values into hidden form fields anywhere on the page or into data-day, data-month, data-year attributes of a relevant object on the page and fetch the values using jquery
day = $('#hiddenfield_day').val(); //put the var day into the day field of new date, etc
Hope this helps.
Change this line:
austDay = new Date(<?= $year ?>, <?= $month ?> , <?= $day ?>);
That said, keep in mind that Javascript's new Date() month param takes a number in the 0 - 11 range.
I try to modify the keith-wood countdown timer to accept a future unix timestamp and hide the days.
To hide the days you use $('#noDays').countdown({until: liftoffTime, format: 'HMS'}); as the example at http://keith-wood.name/countdown.html under the tab Formats1 says.
I didn't manage yet to make it work. How to modify the code to get a unix timestamp as input and hide the days?
Standard timer
<script type="text/javascript">
$(function () {
var austDay = new Date();
austDay = new Date(austDay.getFullYear() + 1, 1 - 1, 26);
$('#defaultCountdown').countdown({until: austDay});
$('#year').text(austDay.getFullYear());
});
</script>
My try to have as input a unix timestamp (does not work)
<script type="text/javascript">
$(function () {
var austDay = new Date();
austDay = new Date(<?php echo $unixtimestamp; ?>*1000);
$('#defaultCountdown').countdown({until: austDay});
$('#year').text(austDay.getFullYear());
});
</script>
You can use the setTime() function:
var austDay = new Date();
austDay.setTime(<?php echo $unixtimestamp * 1000; ?>);
Edit: added * 1000 to go from unix timestamp to javascript timestamp...