I have an edit button for users to edit their posted jobs. I want that button to disable or disappear a week after published day.how can I do this in php. thanks advance...
<?php
$timePublished = 1334301250; // unix time when job posted
$afterOneWeek = $timePublished+(60*60*24*7); // 1 week
$dateNow = date('U'); // unix time for Date Now
if($dateNow > $afterOneWeek){ // if Date Now is greater than our "After One Week"
echo "<button>Button is now visible</button>";
} else {
echo "No button, one week not passed";
}
?>
Related
I want to store the first visited date on that browser and after 7 days I want to show some other content on that web page. So how do check that when that user visited the web page the first time? I want to keep that content visible only for 7 days.How do I achieve this with php code ?
Please help me with this.
As suggested, you can use:
cookies
setcookie
Example:
// When a user visit that page, you will check whether a cookie is
// set or not. If not, then you can set a cookie with the current time
// as its value. If already set, then check if its time is greater than
// 7 days or not.
$showContent = true;
$datetime = new \DateTime();
$now = $datetime->format('Y-m-d H:i:s'); //current datetime
//check cookie
if(!isset($_COOKIE['pageVisitTime'])) {
setcookie("pageVisitTime", $now, time() + (10 * 365 * 24 * 60 * 60)); /* using a far future date */
} else {
$pageVisitTime = $_COOKIE['pageVisitTime'];
$visitDateTime = new \DateTime($pageVisitTime);
if($visitDateTime->diff($datetime)->days > 7) {
$showContent = false;
}
}
//Depending on value of $showContent you can control your content
Okay I am sending an activation email link to user's email. My Activation link is working perfect I wants to make that link expire after X minutes.
Here is my code:
$base_url='http://172.16.0.60/WebServices/';
$activation=md5($email.time());
email format to HTML
$mail->Subject = 'Activate Your Account';
$mail->Body = 'Hi, <br/> <br/> We need to make sure you are human. Please verify your email and get started using your Website account. <br/> <br/> '.$base_url.'activation/'.$activation.'';
Now my
.htaccess file
RewriteEngine On
RewriteRule ^activation/([a-zA-Z0-9_-]+)$ activation.php?code=$1
RewriteRule ^activation/([a-zA-Z0-9_-]+)/$ activation.php?code=$1
Now how can I make my link expire? I dont have timestamp field in my database.
I dont have timestamp field in my database.
Then add one.
Add a field to record when the link was generated and you can then instantly check against that time when they visit. There is no need to waste time trying to think of hacks when you can simply adjust your setup accordingly.
I got my answer using this simply,
Send a timestamp in link and on other end while accepting the link I have to check the time with current time stamp.
$base_url='http://172.16.0.60/WebServices/';
$activation=md5($email.time());
$time = time();
email format to HTML
$mail->Subject = 'Activate Your Account';
$mail->Body = 'Hi, <br/> <br/> We need to make sure you are human. Please verify your email and get started using your Website account. <br/> <br/> '.$base_url.'activation/'.$activation.$time.'';
While when user clicks on link, following code will check the expiration of link.
$u_time = $this->input->get('time'); // fetching time variable from URL
$u_time = md5($u_time);
$cur_time = time(); //fetching current time to check with GET variable's time
if ($cur_time - $u_time < 10800)
{
// link is not expired
}
else
{
// link has been expired
}
date_default_timezone_set("Asia/Kolkata"); // set time_zone according to your location
$expire_date = "2019-03-26 14:45:00"; // time that you want the link to expire
echo $now = date("Y-m-d H:i:s"); // your current time.
if ($now>$expire_date) {
echo " Your link is expired";
}
else
{
echo " link is still alive";
}
you can find your time_zone here
Hope this helps:)
I want to show user that he has vaccinated his goat after successfully payment confirmation.
Here is what I tried:
<?php
$treatperiod1=$product_row['treat1'];
$currentDate = date("Y-m-d");
$totalDays = $treatperiod1;
$usedDays = round(abs(strtotime($currentDate)-strtotime($orderdate))/60/60/24);
$remainingDays = $totalDays-$usedDays;
if($remainingDays==0) {
echo "<a target = '_blank' href ='product_addon.php?treatp=$treatperiod1' class='btn btn-success'><i class='fa fa-pencil'></i>Vaccinate Gaoat</a>";
} elseif($remainingDays==$treatperiod1) {
echo 'Vaccinated';
} else {
echo $remainingDays.' Days Left';
}
?>
I have displayed remaining days for vaccination intervals I want to also display 'Vaccinated' if user payment for vaccination is confirmed. $product_row['treat1']; is a column where the number of days for vaccination is specified. order_details is a table for orders with column confirm for confirmed orders.
Goat Vaccination:
Because Goats don't get Autism
Please read how to use PHP DateTime objects because they will make your life a lot easier on this project.
I will rewrite your code and then tell you what the new code does:
//establish DateTime objects.
$currentDate = new DateTime(); //now.
$orderDate = new DateTime($orderdate); //$orderdate format Y-m-d
//Establish how may days since order date and today.
$difference = $orderDate->diff($currentDate);
$usedDays = $difference->days;
/***
Total Period paid for,
in days, integer.
$product_row['treat1'];
***/
$remainingDays = $product_row['treat1'] - $usedDays;
//Returns an integer value of remaining days.
if($remainingDays < 1) {
echo "<a target = '_blank'
href ='product_addon.php?treatp=".$treatperiod1."'>
Vaccinate Goat</a>";
} elseif($remainingDays==$product_row['treat1']) {
//This will only fire if the goat was vaccinated TODAY.
echo 'Vaccinated';
} else {
echo 'Vaccinated:' .$remainingDays.' Days Left';
}
Hopefully with the help of the link above you should be able to see the basics of what I wrote out for you.
Some notes:
Stop making variables that are simply copies of other variables, it's inefficient and confusing.
(Try to) Stop sending data to new PHP pages as GET URL values such as /newpage.php?treatp=".$var.". This is very probably very insecure.
A more specific answer will need a full explanation from you of your MySQL tables, if nessecary please edit your question and add them.
Try and move away from using timestamps and treating timestamp variables as mathematical entities, and use the DateTime object instead.
Really, to determine if a payment is made you should have a payment date value in your database (set when a payment is successful) and simply compare this date to the value of the integer of the number of days payment covers, using the ->diff() method illustrated above.
Further information on Goat Vaccinations
I have a wordpress site that uses the WPMU Membership plugin. I want to add code to my template that displays a new post to members 5 days before it is shown to non-members.
Something like this:
if (current_user_on_level(17) && CODE TO DETERMINE IF POST IS LESS THAN 5 DAYS OLD) {
echo POST CONTENT;
}
else {
echo 'Info not available yet';
}
;
What code would I use to determine whether the published post is less than 5 days old??
I figured out the answer. First had to convert the post time to a UNIX code using this code:
(True sets it to UNIX epoch time rather than set to local time zone)
Then I used this code to get the UNIX time 5 days ago
strtotime("-5 day")
Put the two together with an if statement that displays the content or if it's not time it displays a message that the content isn't available yet.
<?php
$post_timestamp = get_post_time('U', true);
if (current_user_on_level(17) && $post_timestamp <= strtotime("-1 day"))
{
get_template_part( 'content', get_post_format() );
}
else {
echo 'No post available yet <br />';
}?>
The only issue with this is that because my posts are ordered in chronological order, the top of my blog is filled with the line "No post available yet". Can anyone think of how I can get these statements to sit at the bottom?
I set the users timezone in the header of my page, in my setting area though where a user can pick a timezone, I am trying to show the actual time in my dropdown list of time zones.
the current time is already changed from me setting the pages timezone above, so the already changed time is getting changed again
<?PHP
date_default_timezone_set($_SESSION['time_zone']);
?>
Above set the time zone, below is my code that runs through an array of time zones and builds a form dropdown list, it will have all my time zones and show which one I currently have saved into the session variable. It also shows the current time next to the name to each time zone in my list.
The problem is, I think the previous set time zone for the page, the code above, I think it is messing the times in my dropdown list, If I show the current time on the page, it is correct to my time zone, if I look at times in the dropdown list, they are hours off, any idea how I can do this?
<?PHP
// the time zone array would be here
echo '<select name="time_zone_list" id="time_zone_list"/>';
$_SESSION['time_zone'] = (isset($_SESSION['time_zone'])) ? $_SESSION['time_zone'] : '';
// the current time is already changed from me setting the pages timezone above, so the already changed time is getting changed again
$current_date = date("h:i:s A");
foreach ($time_zone_array as $tz_id => $tz_name) {
$selected = "";
if ($tz_id == $_SESSION['time_zone']) $selected = ' selected="selected"';
//build time to show user in dropdown
$dt_obj = new DateTime($current_date." UTC");
$dt_obj->setTimezone(new DateTimeZone($tz_id));
print '<option value=' . $tz_id . $selected . '> ' .date_format($dt_obj, 'h:i:s A'). ' ' . $tz_name . '</option>';
}
echo '</select>';
?>
I was able to get it working correctly by changing date() to gmdate() for my current time