-1

I'm trying to display the year a website's domain was created using PHP. Just the year of the webpage it's being displayed on, nothing else.

So far I found this code by Greg Randall that is very close to what I'm looking for.

<?php
$url = "https://example.com/";
echo "Domain Create Date of $url " . date( 'F Y', strtotime( domainCreate($url) ) ) . " (" . domainCreate($url) . ")";

function domainCreate($url){
    $parsed = parse_url( $url );
    $domain = trim( $parsed[ 'host' ] );

    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, "http://rdap.org/domain/$domain" );
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    $data = curl_exec( $ch );
    $data = json_decode( $data, true );
    curl_close( $ch );

    return( $data[ 'events' ][ 0 ][ 'eventDate' ] );
}

?>

Output: Domain Create Date of https://example.com/ August 1995 (1995-08-14T04:00:00Z)

I need it to reference the website it's being displayed on. Perhaps ($_SERVER['HTTP_HOST']) ? And only output the year.

Add'l notes: I did a Google search and found the example above in a different post. When I posted a reply asking how to get the code to reference the current page, a mod deleted it. So that is why I had to create my own question. I've tried many variations and continue to get parsing errors. I've been reading and researching all day, I'm still learning. I've run out of steam and decided to ask for help.

  • 1
    `date( 'Y'` would output just the year. This is documented clearly and there are plenty of examples online already, you can easily Google it too – ADyson Aug 29 '23 at 22:47
  • 1
    `Perhaps`...well why not try it and find out? Or, again, do some simple research? See also [ask] – ADyson Aug 29 '23 at 22:48
  • 3
    You're going to make an HTTP request to a third party for every single page load, to fetch one number which is never going to change…? – deceze Aug 30 '23 at 05:36
  • 2
    _"When I posted a reply asking how to get the code to reference the current page, a mod deleted it."_ - this is a Q&A site, and therefor you are not supposed to post something as an A(nswer), if it isn't really one. – CBroe Aug 30 '23 at 06:24
  • Does this answer your question? [Get current domain](https://stackoverflow.com/questions/10717249/get-current-domain) – ADyson Aug 30 '23 at 14:54
  • @ADyson the question is how to display the creation date of the webpage the code is being displayed on. I know how to get the current domain. The issue is implementing it into the code above correctly so I don't receive HTTP 500 parsing errors. – Shamrox425 Aug 30 '23 at 19:45
  • 1
    `I know how to get the current domain`. ok that wasn't clear. Because, for example, in the question text you asked `I need it to reference the website it's being displayed on. Perhaps ($_SERVER['HTTP_HOST']) ?` which very much suggests that you didn't know how to do it! – ADyson Aug 30 '23 at 21:38
  • 1
    `so I don't receive HTTP 500 parsing errors`...what exact error message are you receiving, then? If you get a 500 error, there must be a PHP error message to go with it (either on the screen, or in the PHP error log file). This is new information which you didn't mention before. – ADyson Aug 30 '23 at 21:39

1 Answers1

0

Firstly you are return the first item of $events in JSON, but it might not a registration date. To fix this, you need revise your code

return( $data[ 'events' ][ 0 ][ 'eventDate' ] );

to this

$events = $data[ 'events' ];
$registrationDate = null;
forearch( $events as $event ){
    if( !isset($event['eventAction']) || $event['eventAction'] != 'registration' ){
        continue;
    }
    if(isset($event['eventDate'])){
        $registrationDate = $event['eventDate'];
        break;
    }
}

return $registrationDate;

Remember that, seems there are accept the domain only without sub-domain, means it should be example.com instead of www.example.com.

Finally, suggest you store the date into variable

echo "Domain Create Date of $url " . date( 'F Y', strtotime( domainCreate($url) ) ) . " (" . domainCreate($url) . ")";

to:

$domainCreationDate = domainCreate($url);
echo "Domain Create Date of $url " . date( 'F Y', strtotime($domainCreationDate) ) . " (" . $domainCreationDate . ")";
Allen Chak
  • 1,802
  • 1
  • 10
  • 21
  • Thank you for the updates. The code works fine as is, that is not the issue. I'm trying to edit it so it references whatever webpage it is on, not just an example URL. I appreciate the suggestion regarding subdomain though. – Shamrox425 Aug 30 '23 at 04:27
  • If you want to display the year only, then replace the `F Y` to `Y` is ok, check out the link for the detail https://www.php.net/manual/en/datetime.format.php – Allen Chak Aug 30 '23 at 07:05