I am trying to set date from PHP variable (because values are coming from the database) to daterangepicker but it's not working and it's setting the today's date
Code
<?php $start_date = "04-07-2021"; ?>
<?php $end_date = "10-07-2021"; ?>
<input type="text" name="date-range">
<script>
$(document).ready(function() {
var start_date = <?php echo $start_date; ?>;
var end_date = <?php echo $end_date; ?>;
$('input[name="date-range"]').daterangepicker({
startDate: start_date,
endDate: end_date,
locale: {
format: 'DD-MM-YYYY'
}
});
});
</script>
Your dates are strings so you need to put them in quotes when you turn them into JS string literals:
var start_date = "<?php echo $start_date; ?>";
var end_date = "<?php echo $end_date; ?>";
If you don't, they'll be incomprehensible to the JS interpreter and cause a syntax error (which should have been visible in your browser's Console, although you didn't mention it).
Related
i am receving start and end dates from a proc in following format
Array
(
[0] => Array
(
[min_created_date] => 2020-10-28 00:00:00.000
[max_created_date] => 2020-11-11 00:00:00.000
)
)
i have stored the values in below variables with the following outputs resepectively :
$start_range =$slider_range[0]['min_created_date'];
$start_range=substr($start_range, 0, strrpos($start_range, ' '));
$end_range= $slider_range[0]['max_created_date'];
$end_range=substr($end_range, 0, strrpos($end_range, ' '));
2020-10-28
2020-11-11
now i need to pass these dates to a jQuery function , i want to store this date to js variable since few js opetaions have to be performed to these dates further , Below is how i am trying to pass the dates
$(function() {
var startrange = <?php echo $start_range;?>;
var endrange = <?php echo $end_range;?>;
console.log(startrange+'startrange');
console.log(endrange+'endrange');
});
I get following in console :
1982startrange
1998endrange
please guide me how can i pass the dates to JS variables instead of <?php echo $date ?>
You have no other way to publish PHP variables in JavaScript without echo.
A cleaner way to do it to assign PHP variables to JavaScript vars inside a <script> tag so that PHP echo are not everywhere in your JS code.
<script type="text/javascript">
var startrange = '<?php echo $start_range; ?>';
var endrange = '<?php echo $end_range; ?>';
</script>
you have a lot of options, but i love unixtimestamp formats more ^^
PHP
$minCreatedDate = time();
$maxCreatedDate = strtotime('2020-11-11 00:00:00.000');
JS echoed in PHP file
var startrange = new Date(<?= $minCreatedDate ?> * 1000);
Date instance contains a lot of funcs. like "getDay", ...
I am attempting the below to pass the value in my php variable to JavaScript, but I never have the alert display on screen. What is the proper way to do such?
PHP
<?php $date = '20170101'; ?>
JavaScript
var datestring = <? php echo $date; ?>;
var year = datestring.substring(0,4);
alert(year);
Fiddle:
https://jsfiddle.net/3ed6opj3/
The desired result that I want to display in the alert is 2017 - the answers below to this point have all been showing <? p
You have to add " on between the php script
var datestring = "<?php echo $date; ?>";
You need to add quotes
<?php echo "\"".$date."\""; ?>;
For a realtime countdown i use some php variables and echo them in the js like below:
$expire_year = '2016'; // year
$expire_month = '8'; // month ( 8 = August)
$expire_day = '14'; // day (14th)
$expire_hour = '2'; // hour ( 1-24)
$expire_minutes = '12'; // minutes
i use them in the js like blow:
timestamp = new Date(<?php echo $expire_year; ?>, <?php echo $expire_month - 1; ?>, <?php echo $expire_day; ?>, <?php echo $expire_hour; ?>, <?php echo $expire_minutes; ?>),
This works fine!
I use php strtotime to check if the date really has expired:
$quickpollexpiredate = strtotime("August 14, 2016 2:12"); // set an expiration date
So for the countdown timer in js and for the check in php, i have to use 2 different date "structures".
Is it possible to make a check in php so that i have to use only the 5 variables ($expire_year, $expire_month...) with strtotime() or another function?
So actually what i want to achieve is something like this (ofcourse this is wrong, but i hope you understand what i am trying to achieve)
$quickpollexpiredate = strtotime($expire_year, $expire_month, $expire_day, $expire_hour, $expire_minutes); // set an expiration date
If you have a timestamp in php, you can easily use it to set a Date object in javascript. You just need to consider that a timestamp in javascript is in milliseconds and not seconds like in php:
jsTimestamp = new Date(<?php echo $phpTimestamp; ?> * 1000);
or
jsTimestamp = new Date(<?php echo strtotime($yourDateString); ?> * 1000);
Note that new Date(...) also accepts strings, see https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date for more details.
I am trying to disable dates in a jQuery UI Date Picker, it works when I hard code the dates in to the variable in the JS file as follows:
var bookedDays = ["2015-3-7","2015-3-8","2015-3-15"];
In my PHP file I have:
<?php
$testing = "SELECT fromdate, todate FROM messages WHERE listing_id = '".$_GET['listingid']."'";
$resulttesting = mysql_query($testing) or die(mysql_error() . "<br>" . $testing);
while ($rowtesting = mysql_fetch_assoc($resulttesting))
{
$from = $rowtesting['fromdate'];
$to = $rowtesting['todate'];
}
$start_time = strtotime($from);
$end_time = strtotime($to);
$date_list = array($from);
$current_time = $start_time;
while($current_time < $end_time) {
//Add one day
$current_time += 86400;
$date_list[] = date('Y-m-d',$current_time);
}
//Finally add end date to list, array contains all dates in order
$date_list[] = $to;
$date_list_res = '["' . implode('","', $date_list) . '"]';
print_r ($date_list_res);
?>
<script type="text/javascript">
var bookedDays = <?php echo json_encode($date_list_res); ?>;
</script>
When I run a console.log in the JS file for the variable bookedDays I get ["2015-03-05","2015-03-06","2015-03-07","2015-03-08","2015-03-08"] output which is read from the database which is correct but these dates are not disabling in the date picker. Does anybody know where I'm going wrong?
Instead of
<?php echo json_encode($date_list_res); ?>;
Type just
<?php echo $date_list_res; ?>;
Everything should be dandy.
What I'm trying to do is display the local date using PHP. Right now I have a javascript solution that prints the month that corresponds with the php variable for the month in whatever language I want. For example for <?php echo $one; ?> (January) it can be populated with January (english), Enero (spanish), or Gennaio (italian).
Is there any way to convert this bit of code completely to PHP? if not, is there any way I can turn the code into a php variable so I can just put something like <?php echo $date; ?> and still define $one, $two, $three, etc?
<script type="text/javascript">
var month = new Array();
month[0] = "<?php echo $one; ?>";
month[1] = "<?php echo $two; ?>";
month[2] = "<?php echo $three; ?>";
month[3] = "<?php echo $four; ?>";
month[4] = "<?php echo $five; ?>";
month[5] = "<?php echo $six; ?>";
month[6] = "<?php echo $seven; ?>";
month[7] = "<?php echo $eight; ?>";
month[8] = "<?php echo $nine; ?>";
month[9] = "<?php echo $ten; ?>";
month[10] = "<?php echo $eleven; ?>";
month[11] = "<?php echo $twelve; ?>";
//Array starting at 0 since javascript dates start at 0 instead of 1
var mydate= new Date()
mydate.setDate(mydate.getDate())
document.write(""+month[mydate.getMonth()]+" "+mydate.getDate()+", "+mydate.getFullYear());
</script>
Sorry for not having a fiddle, I would create one but since there is some php involved I'm not sure of where to go for that. If there is something like jsfiddle that also includes php ill set that up if needed so its much easier to understand.
In PHP, you can use setlocale() along with strftime().
So, for example if you want to print the current month in French, you could do:
setlocale(LC_TIME, "fr_CA");
echo strftime("%B");
List of Locales: List of All Locales and Their Short Codes?