I want to show "Stay Tuned..." if there are no Upcoming Events.
Sure there is a simple solution here, I've just never worked with the combination of PHP and shortcodes before.
For reference: http://docs.wp-event-organiser.com/shortcodes/events-list/
Upcoming Events
[eo_events event_start_after=today showpastevents=false] %event_title% on %start{jS M Y}{ g:i:a}% [/eo_events]
PastĀ Events
[eo_events event_end_before=today] %event_title% on %start{jS M Y}{ g:i:a}% [/eo_events]
Be aware that the showpastevents parameter will be deprecated some time soon, so might be good to leave it out.
There are a few ways to achieve this. Here's some raw code that will achieve the desired results (using the shortcode; of course you could do the same thing just using functions):
// GET FUTURE ------------------
if (function_exists("eo_get_events")) {
$events = eo_get_events(array(
'event_start_after'=>'today',
));
if ($events) {
echo do_shortcode("[eo_events event_start_after=\"today\"]%event_title% on %start{jS M Y}{ g:i:a}% [/eo_events]");
} else {
echo "Stay Tuned....";
}
}
// GET PAST ------------------
if (function_exists("eo_get_events")) {
$events = eo_get_events(array(
'event_end_before'=>'today'
));
if ($events) {
echo do_shortcode("[eo_events event_end_before=\"today\"]%event_title% on %start{jS M Y}{ g:i:a}% [/eo_events]");
} else {
echo "No Past Events....";
}
}
Related
i made simple PHP code to show uniquePageViews for my website. The main code was for 7days, but i want to make it lifetime and when i made start-date: "2017-10-5" it start shows me errors. But documentation says that i can place date in that type.
Work variant:
try {
$result = $service->data_ga->get( $GA_VIEW_ID, '100daysAgo', 'today','ga:uniquePageviews');
$count = $result->totalsForAllResults['ga:uniquePageviews'];
echo $count;
} catch(Exception $e) {
var_dump($e);
}
Fail variant:
try {
$result = $service->data_ga->get( $GA_VIEW_ID, '2017-10-5', 'today','ga:uniquePageviews');
$count = $result->totalsForAllResults['ga:uniquePageviews'];
echo $count;
} catch(Exception $e) {
var_dump($e);
}
From the documentation you linked (Emphasis mine) :
All Analytics data requests must specify a date range. If you do not include start-date and end-date parameters in the request, the server returns an error. Date values can be for a specific date by using the pattern YYYY-MM-DD or relative by using today, yesterday, or the NdaysAgo pattern. Values must match [0-9]{4}-[0-9]{2}-[0-9]{2}|today|yesterday|[0-9]+(daysAgo).
Which means your day must be displayed with two digits. Try 2017-10-05 as your start date.
Im making Biogram on my site which lists known historical figures.
Right now, Im just getting values from database, and on many IF statements, determine what to display.
/* #var $bio \Models\Library\Biography */
$birth = [];
$death = [];
$date = null;
if($bio->getBirthMonth() != null) {
if ($bio->getBirthDay() != null) {
$birth = [
$bio->getBirthDay(),
$bio->getBirthMonth(),
$bio->getBirthYear(),
];
} else {
$birth = [
$bio->getBirthMonth(),
$bio->getBirthYear(),
];
}
} else {
$birth = [
$bio->getBirthYear(),
];
}
if($bio->getDeathMonth() != null) {
if ($bio->getDeathDay() != null) {
$death = [
$bio->getDeathDay(),
$bio->getDeathMonth(),
$bio->getDeathYear(),
];
} else {
$death = [
$bio->getDeathMonth(),
$bio->getDeathYear(),
];
}
} else {
$death = [
$bio->getDeathYear(),
];
}
if (!array_filter($birth) && array_filter($death)) {
$date = 'zm. ' . implode('.', $death);
}
if (array_filter($birth) && !array_filter($death)) {
$date = 'ur. ' . implode('.', $birth);
}
if (!array_filter($birth) && !array_filter($death)) {
$date = null;
}
if (array_filter($birth) && array_filter($death)) {
$date = implode('.', $birth) . ' - ' . implode('.', $death);
}
But first of all, Im not happy with this kind of code (don't know if I can write it better).
Secondly when im using Carbon (for example), and want to display year from medival ages, it looks like 0552 instead of 552.
The last thing is that there is no "year 0" in history. There were year 1 After Christ and 1 Before Christ. So when I want to have year -200 it gives me -199.
I know this may be handled by adding -1 to date if it's BC, but I think this is not the right way.
Is there any php library for DateTime that handles all the Dates well?
Also can I, and if so; how to rewrite this code above to look better?
Cheers :)
This is a very interesting problem.
Firstly your if statements - I would write a helper function within $bio that contains the logic regarding what to display as your date, making reference to the other properties that exist within $bio. You could call it getPrettyDate() or something like that. That's potentially where your logic for getting the year to display correctly would go too.
After some considerable googling and not much luck, this answer seems to be the most helpful on this subject, certainly some food for thought.
It's also worth noting that using DATE when persisting this information to your database (assuming you're using MySQL, which you may well not be) is probably not the way forward in your case - as noted in that answer, the MySQL definition of DATE is the following
DATE
A date. The supported range is '1000-01-01' to '9999-12-31'.
I have also after writing this just stumbled across this answer which might be the best approach. Good luck!
More reading on ISO-8601 in the context of 'Year 0' here.
I am preparing a small php attendance script which will record the time and date of given user.
I am trying to display a message that if person is LATE after given time php to display a message you are late
$CURRENTTIME = new DateTime($data['current_time']);
$CURRENTTIME = $CURRENTTIME->format('H:i:s');
$OFFICETIME = new DateTime('10:20:00');
$OFFICETIME = $OFFICETIME->format('H:i:s');
var_dump($CURRENTTIME);
var_dump($OFFICETIME);
if ($CURRENTTIM > $OFFICETIME) {
echo 'you are late tody';
} else {
echo 'Thank you for being on time';
}
also i wanted to learn the code if i need a result in between time? let say i need to display result if employee is in between 10:00:00 - 10:30:00
DateTime objects are comparable so no need to compare their formatted strings. Just compare the objects themselves.
$CURRENTTIME = new DateTime($data['current_time']);
$OFFICETIME = new DateTime('10:20:00');
if ($CURRENTTIME > $OFFICETIME) {
echo 'you are late tody';
} else {
echo 'Thank you for being on time';
}
See this answer for how to see if a time is between two times. It's pretty much the same concept.
Sidenote: There was a typo in your code. $CURRENTTIM which should read as $CURRENTTIME.
Using the following code I am able to get the logs of calls and SMS's. How do I modify this code to only search between certain dates using PHP?
// Instantiate a new Twilio Rest Client
$client = new Services_Twilio($AccountSid, $AuthToken, $ApiVersion);
// http://www.twilio.com/docs/quickstart...
try {
// Get Recent Calls
foreach ($client->account->calls as $call) {
echo "Call from $call->sid : $call->from to $call->to at $call->start_time of length $call->duration $call->price <br>";
}
}
catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
You will want to add a code snippet that looks something like this:
$client = new Services_Twilio('AC123', '123');
foreach ($client->account->calls->getIterator(0, 50, array(
'StartTime>' => '2012-04-01',
'StartTime<' => '2012-05-01'
)) as $call) {
echo "From: {$call->from}\nTo: {$call->to}\nSid: {$call->sid}\n\n";
}
If you want to filter the list, you have to construct the iterator yourself with the getIterator command. There's more documentation here: Filtering Twilio Calls with PHP
User search terms StartTime> and StartTime< for this. First one means call start time is greater than and last one means call start time is less than.
To find every calls that started between 4th and 6th July of 2009 add search term
array(
'StartTime>' => '2009-07-04',
'StartTime<' => '2009-07-06'
)
See example 4 on the twilio doc.
Also note you can always ask twilio support. They usually help gladly.
I am running subscriptions on my website.
I have a 1,3,6 and 12 months of subscription, and I would like the user to be able to change that subscription whenever they feel like.
However, I need to calculate the amount of money the user had to pay had he or she signed up for the shorter term, rather the relatively cheap,longer one.
I made this function optimized_subscription_total($active_sub_time,$arr_sub_values) so that it returns that sum of money exactly.
<?php
function optimized_subscription_total($active_sub_time,$arr_sub_values)
{
// This function takes a row from the DB where prices for each period of time usage is listed. there are prices for 1 month, 3 months,6 and 12 months.
// when the user has subscribed for 12 months, and the user asks for a refund, after they used 9 months and 6 days for example, the system treats the refund as if they subscribed for (in months) COST_6 + COST_3 + (COST_1/30)*6
// the result of the function is then subtracted from the amount they actually paid and is considered the refund.
// $arr_sub_values is the associative row from the DB, containing the prices
// $active_sub_time is measured in months and is a double
$result=0;
while(($active_sub_time-12)>=0)
{
$active_sub_time-=12;
$result+=($arr_subscription_values['COST_12']);
}
while(($active_sub_time-6)>=0)
{
$active_sub_time-=6;
$result+=($arr_subscription_values['COST_6']);
}
while(($active_sub_time-3)>=0)
{
$active_sub_time-=3;
$result+=($arr_subscription_values['COST_3']);
}
while(($active_sub_time-1)>=0)
{
$active_sub_time-=1;
$result+=($arr_subscription_values['COST_1']);
}
if($active_sub_time>0)
$result+=($active_sub_time)*($arr_subscription_values['COST_1']);
return $result;
}
$datetime1 = date_create('2009-12-11');
$datetime2 = date_create('2010-11-09');
$interval = date_diff($datetime1, $datetime2);
$num_of_months = ($interval->format('%y'))*12+($interval->format('%m'))+($interval->format('%a'))/30;
echo "<br />";
$v = array('COST_1'=>'3.99','COST_3'=>'9.99','COST_6'=>'15.99','COST_12'=>'25.99');
echo "OPT value for $num_of_months months=" . optimized_subscription_total($num_of_months, $v);
?>
Strangely I get the bug appearing only after 7 to 10 times after refreshing this code.
So I got:
OPT value for 10 months=M.97
as a result here. I think I need to get a float number, no ?
I was expecting the result of the function that should be "OPT value for 10 months=29.97", as it should take COST_6 + COST_3 + COST_1... but I get that weird M.97, and sometimes things like POKHHHG.97
I would change the logic to the following and see if error is still produced. I think this is a little bit more clear and easy to debug. It is the same as your though just explained differently.
while($active_sub_time>=12)
{
$active_sub_time-=12;
$result+=($arr_subscription_values['COST_12']);
}
while($active_sub_time>=6)
{
$active_sub_time-=6;
$result+=($arr_subscription_values['COST_6']);
}
while($active_sub_time>=3)
{
$active_sub_time-=3;
$result+=($arr_subscription_values['COST_3']);
}
while($active_sub_time>=1)
{
$active_sub_time-=1;
$result+=($arr_subscription_values['COST_1']);
}
What I would also do is added debug cod at the top inside function.
echo "<br>$active_sub_time" // debug code include at the top
This will give you an idea, if any garbage being is being sent to the function itself.
Also I would add this test function in all while blocks if the above does not resolve the issue.
if (!is_numeric($result))
{
echo"<br> Bug occurred";break; // print other values if necessary
}