I hope somebody can help work out to do this. Basically, I have a php script which displays youtube videos and statistics from the youtube data json api. The thing is, I need to be able to show the total view count for the previous month as well as the current month. I can get the total view count for a video and if you look on youtube there is a graph underneath the video which shows the views for a few months. Is this possible?? If not, can is there any other way I could go about storing the previous months view count?
Thanks in advance.
Run a cron job that updates a monthly tick...
All you would need to do is store the value of views at the beginning of each month and when the next month arrives you calculate the difference.
Running the cron job would ensure that 30 days are up and that the values should be updated.
Ex.
$lastmonth = 2238923; // 2,238,923views at the start of this month
$currentview = youtubeview(); // However you get the current view
$thismonth = $lastmonth - $currentview; // Difference and when the month is over $thismonth becomes $lastmonth :)
Related
I want to create a website with an scheduling calendar.
My first idea is to use some free calendar template or download some free scheduling calendar. Then in my scheduling form, when someone request for an schedule, I will get the date he/she input and save it into the database then show it to the scheduling calendar.
But someone told me that, in my database, I should create a calendar table.
Which is the best way around?
The first one with only one table for schedule on my database or the second one with two tables for schedule and calendar?
I hope you get my idea.
It could be first one. One of option is to keep data by day of year.
you can draw your own calendar by counting day of year
actual day of yaer - date('z') + 1; //+ 1 because it is an array it starts from 0
then you can get number of days in each month
cal_days_in_month
and loop it x 12 with
here will be day of month with css style so it looks like calendar field
your i++ will bee number of days in month of course.
Keep records in database by year and day of year. you can do so much things this way
I am working on a school project where I am keeping track of a user's tweeting frequency per week. I have working code, but at the end of each 1-week period, I need to manually adjust the new starting tweet total and the date of one week in the future.
How can I automate it so the final tweet count becomes the new starting tweet count, and one week gets added to the ending date? Am I heading in the right direction with the code below, or should I be storing these final tweet total values in a database? Thank you!
// Get current tweet total and calculate current count
$ptTotal = $ptObject->{'statuses_count'};
$ptStart = 572;
$ptCount = ($ptTotal-$ptStart);
// Set end date & convert to EST
$ptdatestr="2017-05-30 12:00:00";
$ptdate=strtotime($ptdatestr)+14400;
// Calculate time remaining
$ptdiff=$ptdate-time();
$ptdays=floor($ptdiff/86400);
$pthours=round(($ptdiff-$ptdays*86400)/3600);
// Re-set start value and add one week to countdown
if ($ptdiff <= 0) {
$ptStart = $ptTotal;
$ptdate = $ptDate + 604800;
}
I say regardless of how you are automating this code block (see Alejandro's comment), you should move away from using any approach that includes +86400 (or a factor of). Things will go BONK in the night when daylight savings is involved.
Instead, I recommend that you integrate DateTime objects. They are highly versatile and have specific features that will aid you in your specific project. This is a full list of related functions: http://php.net/manual/en/book.datetime.php
Implementing Datetime objects and functions will make your project solid and lean. Immerse yourself in the above php manual pages and the comments that follow; and continue to research on StackOverflow.
More to your specific questions: Yes, I think you are on the right path. Yes, I think I'd store the data in a database.
Good luck.
I've had many issues trying to find the problem to no hope.
I have a user stats page where it shows their all-time stats but would like to add a weekly and monthly stat page. Using SQL or PHP is there a way to get the total number from Monday to Friday. It needs to be from Monday to Sunday then resets the count back to 0 on Sunday. Selecting from one date range to another won't work because it'll have to change dynamically or is that not possible?
I want the results to be like this:
Table structure as shown:
http://imgur.com/a/YZOuy
if you could help it would be much appreciated. thanks!
I'm having lots of trouble building an appointment booking site for personal trainers. To understand fully why I'm having so much trouble please be patient and let me explain. I wanted to use this free responsive calendar for the look and feel of my calendar because it's responsive and I like the way it looks, although I'll make some minor alterations to it like color scheme. If you go to the link below you will see a demo of it:
http://tympanus.net/Development/Calendario/index2.html
If you click back on the calendar arrow to November 2012, you will see that certain days have circles on them which indicate that there are events on those days. If you click on the circle a window will come up which is supposed to provide info about the event. The events are fetched from a JSON file whose format is the date for the keys and the values are URLs. When a user clicks on an event instead of the window coming up I want them to be taken to a separate page which will list the available time slots for the trainer on that particular day. Their availability will be based on two things:
1) The schedule that they set for themselves like for instance Mondays, Wednesdays, Fridays from 10am-5pm. I would provide a form on the site where they could set their schedule like this and then save it to a MySQL database and query that database to populate the JSON file.
2) What appointment slots are booked on available days. This is complicated since appointment times can vary. A session can last 15, 30, 45 minutes, 1 hour, 2 hours and 30 minutes, etc. All in 15 minute chunks. So for instance if the session that the user selected is a 1 hour session, I would have to query the schedule of the personal trainer and match that against his appointments booked on every single day and try to figure out where he has 1 hour slots available then show them to the user on a separate page after they've clicked on the calendar date.
I'm having lots of difficulty figuring this out. I can populate the JSON file by querying the schedule of the personal trainer and then using a trick similar to the one below to get all the dates for the next year for a certain day that the trainer is available like for instance Saturday below. Then store that info in the JSON file which will fetch the events for the calendar script:
<?php
$day = 'saturday';
$step = 1;
$unit = 'W';
$start = new DateTime('NOW');
$end = clone $start;
$start->modify($day); // Move to first occurence of day
$end->add(new DateInterval('P1Y')); // Move to 1 year from start
$interval = new DateInterval("P{$step}{$unit}");
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
echo $date->format('D, d M Y'), PHP_EOL;
echo "<br />";
}
?>
Why would you want to send them to a separate page, when the whole idea of this calendar is to load events inline?
In your json I would only pass the available timeslots for a given day, in order to keep your frontend logic as basic as possible. Figure out serverside what timeslots are still available by walking through that days' events. This is fairly straightforward (sort the appointments, measure the time between the end of event x and the start of event x+1).
Beware of concurrency.
Storing and retrieving schedules and appointments (have you considered recurring appointments too?) can get complicated. You could read up on Martin Fowler, who has an excellent article on modelling schedules and calendars.
In my database, there are some days with data and some without; I have one column with data, and another column with the date and time submitted. So I want to create this calendar. So it will show all the days in the month, and then the days with data will be a hyperlink.
When I click the link, it will show all the data submitted on that day. I would probably use a loop within the first loop. One for month, and then one for displaying each and every day.
However, being that each month of the year has different amount of days and also leap year is a problem, I don't know how to write the conditions.
Any help or guidance is appreciated.
$start = '2009-01-01';
$current = strtotime($start);
while(date('n',$current)==date('n',strtotime($start))) {
// do your stuff here, $current includes the current date.
// the loop will run through the complete month
$current = strtotime(date('Y-m-d',$current) . '+1 day');
}
You'll need to first find out what day of the week the month starts, so you know how many empty boxes to spit out. Then figure out how many days in that month. Then loop through your days, wrapping to the next line after Saturday. Then fill in the rest of the last row with empty boxes.
There's some quite simple (and commented) code that does this here:
http://gitorious.org/wfpl/wfpl/blobs/master/calendar.php
You can use that code without the rest of the framework it's designed for, if you simply rewrite calender_day() (which is called for each cell on the calendar, with it's first parameter telling you what sort of day it is (not in month, has events, eventless) and rewrite calendar_week() which is called at the end of each row.
Or you could just look through for how to do relevant stuff, like find out how far through the week the month starts and such.