Shamrox425
Shamrox425

Reputation: 1

How to display webpage's domain creation year in PHP

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.

Upvotes: -1

Views: 68

Answers (1)

Allen Chak
Allen Chak

Reputation: 1960

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

Upvotes: 1

Related Questions