How can I automatically insert date and time to a meta tag on page load.

Castamir

BANNED
Joined
Sep 4, 2019
Messages
1,200
Reaction score
1,058
How can I automatically insert date and time to a meta tag on page load? What's the best code snippet for this?
 
Something like this:

https://codepen.io/phpcodertech/pen/oNbrqee
Wow, this is so useful. So, I can pass the entire script in between the meta value for each meta key where time is needed?
 
With PHP. Just use it to print the date and time into the meta tag. It’s server side language so will essentially inject there before the page load.
 
With PHP. Just use it to print the date and time into the meta tag. It’s server side language so will essentially inject there before the page load.
If you have a code snippet, you can post it please for others. Meanwhile, I believe the codepen from @Slippers looks promising, or do you think it needs improvement?
 
I was able to figure this out later on. Meta tags on pages use the opengraph protocol which requires a w3c time format, and when I tried configuring the Javascript codepen from Slippers, I didn't get it right.
So, I went to stackoverflow.com and found the code snippet below, the only thing I changed is the w3c time format. I am posting this here in case someone stumbled across this thread later on
PHP:
<meta property="article:published_time" content="2020-01-06T11:03:26+00:00" /><meta property="article:modified_time" content="@php date_default_timezone_set('Africa/Lagos');

$timestamp = time();
$date_time = date("Y-m-d\TH:i:sP", $timestamp);
echo "$date_time"; @endphp" />

EDIT : Remove @php and @endphp if you are not using laravel and replace with normal php opening and closing synthax.
 
I was able to figure this out later on. Meta tags on pages use the opengraph protocol which requires a w3c time format, and when I tried configuring the Javascript codepen from Slippers, I didn't get it right.
So, I went to stackoverflow.com and found the code snippet below, the only thing I changed is the w3c time format. I am posting this here in case someone stumbled across this thread later on
PHP:
<meta property="article:published_time" content="2020-01-06T11:03:26+00:00" /><meta property="article:modified_time" content="@php date_default_timezone_set('Africa/Lagos');

$timestamp = time();
$date_time = date("Y-m-d\TH:i:sP", $timestamp);
echo "$date_time"; @endphp" />

EDIT : Remove @php and @endphp if you are not using laravel and replace with normal php opening and closing synthax.
Great, that's another way to do it
 
Back
Top