Converting This Javascript to PHP to display Local Date? - javascript

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?

Related

Set date from PHP variable to daterangepicker

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).

Pass PHP Variable To JavaScript And Use Alert To Display

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."\""; ?>;

Looping with php over a php-Statement wrapped in javascript

I am using dygraphs to plot a graph. I am using a database to store all the values i need. to use dygraphs i have to use javascript. but the data im getting from the database needs to fill the dygraphs constructor dynamically. i could do it like this, but that isnt dynamic and not what i need.
<script type="text/javascript">
g = new Dygraph(
document.getElementById('graphdiv'),
"Travel(mm),Force(N)\n"+
"0,0\n"+
"<?php $sql = "SELECT daten FROM azubi3 where pau_table_id=2";
$result = pg_query($sql);
$row = pg_fetch_row($result);
$toEcho = str_replace("{","[",$row[0]);
$toEcho = str_replace("}","]",$toEcho);
$toEcho = json_decode($toEcho);
echo $toEcho[0][0].','.$toEcho[0][1].'\n';
?>"+
"<?php echo $toEcho[1][0].','.$toEcho[1][1].'\n';?>"+
"<?php echo $toEcho[2][0].','.$toEcho[2][1].'\n';?>"+
"<?php echo $toEcho[3][0].','.$toEcho[3][1].'\n';?>"+
"<?php echo $toEcho[4][0].','.$toEcho[4][1].'\n';?>"+
"<?php echo $toEcho[5][0].','.$toEcho[5][1].'\n';?>"+
"<?php echo $toEcho[6][0].','.$toEcho[6][1].'\n';?>"+
"<?php echo $toEcho[7][0].','.$toEcho[7][1].'\n';?>"+
"<?php echo $toEcho[8][0].','.$toEcho[8][1].'\n';?>"+
"<?php echo $toEcho[9][0].','.$toEcho[9][1].'\n';?>"+
"<?php echo $toEcho[10][0].','.$toEcho[10][1].'\n';?>"+
"<?php echo $toEcho[11][0].','.$toEcho[11][1].'\n';?>"+
"<?php echo $toEcho[12][0].','.$toEcho[12][1].'\n';?>"+
"<?php echo $toEcho[13][0].','.$toEcho[13][1].'\n';?>"+
"<?php echo $toEcho[14][0].','.$toEcho[14][1].'\n';?>"+
"<?php echo $toEcho[15][0].','.$toEcho[15][1].'\n';?>"+
"<?php echo $toEcho[16][0].','.$toEcho[16][1].'\n';?>"+
"<?php echo $toEcho[17][0].','.$toEcho[17][1].'\n';?>"+
"<?php echo $toEcho[18][0].','.$toEcho[18][1].'\n';?>"+
"<?php echo $toEcho[19][0].','.$toEcho[19][1].'\n';?>"+
"<?php echo $toEcho[20][0].','.$toEcho[20][1].'\n';?>"+
"<?php echo $toEcho[21][0].','.$toEcho[21][1].'\n';?>"+
"<?php echo $toEcho[22][0].','.$toEcho[22][1].'\n';?>"+
"<?php echo $toEcho[23][0].','.$toEcho[23][1].'\n';?>"+
"<?php echo $toEcho[24][0].','.$toEcho[24][1].'\n';?>"+
"<?php echo $toEcho[25][0].','.$toEcho[25][1].'\n';?>"
);
</script>
because i have to seperate the data with "...."+ i dont know how to control that i echo as much as my array is long. i thought of a loop over the javascript code which itself contained php. it didnt work. some ideas? or questions, i dont know if anyone will understand me...
This looks to me like a perfect case for using foreach
Output will look like
"123,123\n;"+
.....
"126,143\n;"
Code:
$setCount = 0; // keeps track of sets so i dont put a linebreak before the first one
$toEcho = json_decode($toEcho);
foreach ($toEcho as $set){
if($setCount>0) echo "+"; // every set is seperated with a plus
echo "\""; // put double quote down
$valueCount = 0;
foreach($set as $value){
if($valueCount>0) echo ',';
echo $value;
$valueCount++;
}
echo "\\n;\""; // puts the weird \n into the string and final quote
$setCount++;
}
Let me know if something is unclear.

Using a PHP variable inside JavaScript

I'm pretty new, so go easy. I am using a session in PHP with a few variables. I want to concatenate two strings together from my PHP session when the clicks a button. I am using JavaScript onclick to try and achieve this.
I am having trouble getting my JavaScript to accept my PHP variables. Does anybody have any suggestions?
PHP:
$_SESSION['user1'] = $_POST['user'];
$_SESSION['string3'] = $_POST['string1'];
$_SESSION['string4'] = $_POST['string2'];
echo $_SESSION['user1'];
echo $_SESSION['string3'];
echo $_SESSION['string4'];
if(isset($_SESSION['user1'])) {
echo 'Welcome, '.$_SESSION['user1'].". Lets concatenate ".$_SESSION['string3']." and ".$_SESSION['string4'];
} else {
echo "<meta http-equiv=\"refresh\" content=\"0;URL=">";
}
<button onclick="xy()">Try it</button>
<p id="answer"></p>
JS:
<script type="text/javascript">
function xy() {
var x = "<?php $_SESSION['string3']; ?>";
var y = "<?php $_SESSION['string4']; ?>";
document.getElementById("answer").innerHTML = x + y;
}
</script>
You need to echo the PHP variable there because otherwise they output nothing and the JS variable ends up empty:
var x = "<?php echo $_SESSION['string3']; ?>";
var y = "<?php echo $_SESSION['string4']; ?>";
So for example, if $_SESSION['string3'] is "abc", PHP will echo the value inside the JS and you'll get this:
var x = "abc";
instead of:
var x = ""; // no output from PHP when there's no echo
To simplify things, here is your whole function:
<script type="text/javascript">
function xy() {
var x = "<?php echo $_SESSION['string3']; ?>";
var y = "<?php echo $_SESSION['string4']; ?>";
document.getElementById("answer").innerHTML = x + y;
}
</script>
Maybe it should be something like this?
echo "<script type=\"text/javascript\">\n";
echo "function xy() {\n";
echo "var x = ".$_SESSION['string3']."\";\n";
echo "var y = ".$_SESSION['string4']."\";\n";
echo "document.getElementById(\"answer\").innerHTML = x + y;\n}";
echo "</script>";

Two identical .load() don't work together

I have these two scripts.
First:
$(document).ready(function(){
refreshStatus();
});
function refreshStatus(){
setTimeout(function(){
$('#div_top_info').load('load_status.php');
}, 2000);
}
Second:
var skill = <?php echo json_encode($skill_, JSON_UNESCAPED_UNICODE); ?>;
var skill_final = <?php echo json_encode($skill_final, JSON_UNESCAPED_UNICODE); ?>;
var camp_final = <?php echo json_encode($camp_final, JSON_UNESCAPED_UNICODE); ?>;
var jmeno = <?php echo json_encode($jmeno_, JSON_UNESCAPED_UNICODE); ?>;
var pozice = <?php echo json_encode($pozice_, JSON_UNESCAPED_UNICODE); ?>;
var delka = <?php echo json_encode($delka_, JSON_UNESCAPED_UNICODE); ?>;
var opravneni = <?php echo json_encode($opravneni, JSON_UNESCAPED_UNICODE); ?>;
$(document).ready(function(){
refreshPrehled();
});
function refreshPrehled(){
$('#checkboxes').load('load_prehled.php', {
skill: skill,
skill_final: skill_final,
camp_final: camp_final,
jmeno: jmeno,
pozice: pozice,
delka: delka,
opravneni: opravneni
}, function(){
setTimeout(refreshPrehled, 12000);
});;
}
Second is below first on my page.
Does this mean only one setTimeout can run on one page?
And if thats the case, how do I put these two together into one script when I wanna load two different .php into two different html objects?
Thanks for help!
EDIT
Also html objects:
<div class="div_top_info" id="div_top_info"></div>
<form action="index.html" method="POST" id="checkboxes"></form>
I have tried different variations of timeouts, setInterval, different positioning, delay one of the timeouts. Really don't know. I fight this second day now.
EDIT2
Important info:
Sorry bad info.
They load, but into each php (load_status and load_prehled) I put this code:
$z_cas_editace = date('Y-m-d H:i', filemtime('datazelva_UL.txt'));
And second one updates the time and first one doesn't.
The first function also works fine when I put it in place of second.
Just my head spinning...
var skill = <?php echo json_encode($skill_, JSON_UNESCAPED_UNICODE); ?>;
var skill_final = <?php echo json_encode($skill_final, JSON_UNESCAPED_UNICODE); ?>;
var camp_final = <?php echo json_encode($camp_final, JSON_UNESCAPED_UNICODE); ?>;
var jmeno = <?php echo json_encode($jmeno_, JSON_UNESCAPED_UNICODE); ?>;
var pozice = <?php echo json_encode($pozice_, JSON_UNESCAPED_UNICODE); ?>;
var delka = <?php echo json_encode($delka_, JSON_UNESCAPED_UNICODE); ?>;
var opravneni = <?php echo json_encode($opravneni, JSON_UNESCAPED_UNICODE); ?>;
$(document).ready(function(){
refreshStatus();
refreshPrehled();
});
function refreshStatus(){
setTimeout(function(){
$('#div_top_info').load('load_status.php');
}, 2000);
}
function refreshPrehled(){
$('#checkboxes').load('load_prehled.php', {
skill: skill,
skill_final: skill_final,
camp_final: camp_final,
jmeno: jmeno,
pozice: pozice,
delka: delka,
opravneni: opravneni
}, function(){
setTimeout(refreshPrehled, 12000);
});;
}

Categories

Resources