Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

149
Visualizações
How to add Javascript date from browser inside a PHP link

I know how to code in PHP but admittedly new to Javascript. I am trying to create a link upon collecting the date. When I echo the date from the JS code, it works but adding it inside a link is somehow proving challenging.

<?php  
if ( ! defined( 'ABSPATH' ) ) exit;

$date = '<script type="text/javascript">
var event = new Date();
var options = { year: "numeric", month: "numeric", day: "numeric" };
document.write(event.toLocaleDateString("es-CL", options));
</script>';

echo $date; // 18-11-2021

//Unable to create a link
echo '<a href="/example/'.$date.'"> Today </a>';

?>

Unsure what my code is missing. Thanks

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use like below to get javascript date in link:

<a href="" id="mydate"> Today </a>

<script type="text/javascript">
var event = new Date();
var options = { year: "numeric", month: "numeric", day: "numeric" };
var date=event.toLocaleDateString("es-CL", options);
document.getElementById("mydate").href="/example/"+date;
</script>
about 4 years ago · Juan Pablo Isaza Relatório

0

This is how you can achieve it using jquery

Step 1: Add Jquery CDN

Step 2: HTML

<a href="javascript:void(0);" class="test">Test</a>

Step 3: Adding Jquery Code and getting current date time

var currentdate = new Date(); 
var datetime = currentdate.getDate() + "/"
                + (currentdate.getMonth()+1);

$('.test').attr('href',datetime); //this is how you can add to href tag
$('.test').text(datetime) //this is how you can add to text of <a> tag

This is how you can achieve it.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda