Help with adding data to an array - syntax error

the web script file that I’m trying to modify shows this:

$get_videos = $db->rawQuery("SELECT DISTINCT(v.id), v.* FROM u_paid_videos AS upv LEFT JOIN " . T_VIDEOS . " AS v ON (upv.id_video = v.id) WHERE upv.id_user = {$user->id} ORDER BY upv.id DESC");

to work with this:

if (!empty($get_videos)) {
    $len = count($get_videos);
    foreach ($get_videos as $key => $video) {
        $video = PT_GetVideoByID($video, 0, 0, 0);
        $pt->last_video = false;
        if ($key == $len - 1) {
            $pt->last_video = true;
        }
        $final .= PT_LoadPage('paid-videos/list', array(
            'ID' => $video->id,
            'USER_DATA' => $video->owner,
            'THUMBNAIL' => $video->thumbnail,
            'URL' => $video->url,
            'TITLE' => $video->title,
            'DESC' => $video->markup_description,
            'VIEWS' => $video->views,
            'VIEWS_NUM' => number_format($video->views),
            'TIME' => $video->time_ago,
            'DURATION' => $video->duration
        ));
    }
}

I tried to add time_date, but I believe I am missing something:

$get_videos = $db->rawQuery("SELECT DISTINCT(v.id), v.* FROM u_paid_videos AS upv LEFT JOIN " . T_VIDEOS . " AS v ON (upv.id_video = v.id) WHERE upv.id_user = {$user->id} AND upv.time_date ORDER BY upv.id DESC;

the console shows “Uncaught SyntaxError: Unexpected token u in JSON at position 0”
not sure how to resolve that

any help will be appreciated

Look at the end of your line compared to the original.

Thanks for your reply. very helpful.

Now that I’ve corrected that, with your help, I have changed this:

'TIME' => $video->time_ago,

to this:

'TIME' => $video->time_date,

but the displayed time-date on the html page shows, for example:

2019-05-22 20:13:23

I like to have it display without the 20:13:23 portion.

The table column time_date structure shows:

name: time_date
type: datetime
null: yes
default: null

what can I modify to have it display without the 20:13:23 portion?
I’ve tried a few changes without success.

any assistance is appreciated

Try finding the type:

echo gettype( $video->time_date );

If it is a string then try:
echo sub_str( $video->time_date, 0, 11 );

and if a timestamp or date field:

echo date( ‘Y-m-d’, $video->time_date);

Check the online PHP manual for the correct syntax for the date (…); function.

https://www.php.net/manual/en/function.date.php

Edit:
Corrected function name get_type(…); to gettype(…);

Thanks for your reply.

I tried adding

echo gettype( $video->time_date );

and the file would not process.

Is there a certain place to add that to the php file?:

<?php

if (IS_LOGGED == false) {
    header("Location: " . PT_Link('login'));
    exit();
}
$user_id               = $user->id;
$pt->is_admin          = PT_IsAdmin();
$pt->is_settings_admin = false;


$list = '<div class="text-center no-content-found empty_state"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-video-off"><path d="M16 16v1a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V7a2 2 0 0 1 2-2h2m5.66 0H14a2 2 0 0 1 2 2v3.34l1 1L23 7v10"></path><line x1="1" y1="1" x2="23" y2="23"></line></svg>' . $lang->no_videos_found_for_now . '</div>';
$list2 = '<div class="text-center no-content-found empty_state"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-video-off"><path d="M16 16v1a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V7a2 2 0 0 1 2-2h2m5.66 0H14a2 2 0 0 1 2 2v3.34l1 1L23 7v10"></path><line x1="1" y1="1" x2="23" y2="23"></line></svg></div>';
$final = '';


//$get_videos = $db->rawQuery("SELECT DISTINCT(v.id), v.* FROM u_paid_videos AS upv LEFT JOIN " . T_VIDEOS . " AS v ON (upv.id_video = v.id) WHERE upv.id_user = {$user->id} ORDER BY upv.id DESC");


$get_videos = $db->rawQuery("SELECT DISTINCT(v.id), v.* FROM u_paid_videos AS upv LEFT JOIN " . T_VIDEOS . " AS v ON (upv.id_video = v.id) WHERE upv.id_user = {$user->id} AND upv.time_date ORDER BY upv.id DESC");


if (!empty($get_videos)) {
    $len = count($get_videos);
    foreach ($get_videos as $key => $video) {
        $video = PT_GetVideoByID($video, 0, 0, 0);
        $pt->last_video = false;
        if ($key == $len - 1) {
            $pt->last_video = true;
        }
        $final .= PT_LoadPage('paid-videos/list', array(
            'ID' => $video->id,
            'USER_DATA' => $video->owner,
            'THUMBNAIL' => $video->thumbnail,
            'URL' => $video->url,
            'TITLE' => $video->title,
            'DESC' => $video->markup_description,
            'VIEWS' => $video->views,
            'VIEWS_NUM' => number_format($video->views),
            'TIME' => $video->time_date,
            //'DURATION' => $video->duration
        ));
    }
}


if (empty($final)) {
	$final = $list;
}

$pt->videos      = $get_videos;
$pt->page        = 'paid-videos';
$pt->title       = $lang->search . ' | ' . $pt->config->title;
$pt->description = $pt->config->description;
$pt->keyword     = $pt->config->keyword;
$pt->content     = PT_LoadPage('paid-videos/content', array(
    'VIDEOS' => $final,
    'KEYWORD' => $keyword
));

Yes. Where did you add it?

The $video object only exists for a short section of your code. If you tried to use it outside that loop, you’d get an error.

Thanks again for your reply.

I placed it here:

        $final .= PT_LoadPage('paid-videos/list', array(
            'ID' => $video->id,
            'USER_DATA' => $video->owner,
            'THUMBNAIL' => $video->thumbnail,
            'URL' => $video->url,
            'TITLE' => $video->title,
            'DESC' => $video->markup_description,
            'VIEWS' => $video->views,
            'VIEWS_NUM' => number_format($video->views),
            'TIME' => $video->time_date
            //'DURATION' => $video->duration
        ));
        echo gettype( $video->time_date );
    }
}

and I see this:

stringstringstringstringstringstringstringstringstringstringstringstringstringstringstringstringstringNULLstring

any additional guidance will be appreciated

OK, well that’s working correctly, so I’m not sure what you mean by “the file would not process”. The gettype() function returns the type of the variable that you pass into it, so in this case it’s telling you that most of them are strings.

Armed with that information, you can use the information that @John_Betong gave you above to format the date in the way that you want it.

1 Like

Thanks for your replies.

It appears that my ‘time_date’ value, in my ‘$get_videos’ line, is coming from the ‘videos’ db table, but I’d like it to come from the ‘u_paid_videos’ db table.

Any additional guidance is appreciated.

Do you have the same column names in both tables? If so, you might need to modify the query to use an “as” to specify an alternate name for one of the columns - I have a vague recollection from when I started looking at PHP that there could be an issue in accessing both values, though that might be out of date information now.

Nope, still relevant :slight_smile:

1 Like

Thanks for all of your replies/help.

This turned out to be the solution:

$get_videos = $db->rawQuery("SELECT DISTINCT(v.id), v.*, upv.time_date as time_date FROM u_paid_videos AS upv LEFT JOIN " . T_VIDEOS . " AS v ON (upv.id_video = v.id) WHERE upv.id_user = {$user->id} AND upv.time_date ORDER BY upv.id DESC");

and it displays, for example "2019-05-29 12:39:17’.

I’d prefer it to display something like 2019-05-29 8:39am PST, if possible.

The sql parameters, presently, for the ‘time_date’ db column are type: TIMESTAMP and default: CURRENT_TIME

The php is:

        $final .= PT_LoadPage('paid-videos/list', array(
            'ID' => $video->id,
            'USER_DATA' => $video->owner,
            'THUMBNAIL' => $video->thumbnail,
            'TITLE' => $video->title,
            'DESC' => $video->markup_description,
            'VIEWS' => $video->views,
            'TIME' => $video->time_date
        ));
    }
}

and the corresponding html is:

<div class="video-info">
<div>
<a href="{{USER url}}" data-load="?link1=timeline&id={{USER username}}">{{USER name_v}}</a> <span class="bold">·</span> <span>{{TIME}}</span>
</div>
</div>

so, I’m still not clear on how to change the current 2019-05-29 12:39:17 display, to instead display: 2019-05-29 8:39am .

Any additional help/clarification is appreciated.

Have a look at the date() and strtotime() functions. Something like

echo date("g:i a F j, Y ", strtotime($video->time_date)) ;

which will give you something like 12:39 am May 29, 2019 for your sample time.

The PHP doc has explanations of all the formatting characters and lots of examples: https://www.php.net/manual/en/function.date.php

I’m not sure how it would change from 12:39am to 8:39am, presumably a timezone thing.

Thanks for your reply.

I had looked at that link prior to posting, but still wasn’t clear on how to integrate that with the script code.

I have added you suggestion here like so:

        $final .= PT_LoadPage('paid-videos/list', array(
            'ID' => $video->id,
            'USER_DATA' => $video->owner,
            'THUMBNAIL' => $video->thumbnail,
            'TITLE' => $video->title,
            'DESC' => $video->markup_description,
            'VIEWS' => $video->views,
            'TIME' => $video->time_date
        ));
        echo date("g:i a F j, Y ", strtotime($video->time_date)) ;
    }
}

the result is attached:

which appears at the top of the html page.

Any help with getting just a single current date similar to that, to appear where {{TIME}} displays on the html page is appreciated.

Well, you’re echoing the value in the code that builds up the array to draw the page. So instead of echoing it, you need to assign it to your “TIME” element a couple of lines above where you have that echo.

I used echo just to demonstrate how it fits together, not to suggest that you should actually echo it.