0

I am using Joomla and attempting to use the substring() function to pull out the first 4 characters of my string. The issue I am having is that I get an error of

This is my syntax - how should I change it so that it functions in my Joomla set-up?

Uncaught TypeError: phpdate.substring is not a function

Here is syntax:

<?php
  $randardate = '20160301';
?>
<script>
  var phpdate = <?php echo $randardate; ?>;
  var yearfromphpdate = phpdate.substring(0,4);
</script>
3
  • 2
    Your PHP evaluates to var phpdate = 20160301; which isn't a string. Commented Jul 18, 2017 at 22:38
  • 1
    var phpdate = '<?php echo $randardate; ?>'; Commented Jul 18, 2017 at 22:40
  • 1
    var yearfromphpdate = phpdate.toString().substring(0,4) Commented Jul 18, 2017 at 22:41

2 Answers 2

1

Add quotation to make phpdate a string.

var phpdate = '<?php echo $randardate; ?>';

Sign up to request clarification or add additional context in comments.

Comments

0

Since you are pre processing a javascript file with PHP, without quotes your javascript file would look something like

var phpdate = 20160301;

You need to add quotations like this

var phpdate = '<?php echo $randardate; ?>';

So that when PHP is done processing your file, it will be a string, not an int.

var phpdate = '20160301';

Your stacktrace is being thrown because substring expects a string, not an int.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.