Time/date link to PHP file when running in different URLs - php

I presently have a situation whereby I need to show the new copy in a staging area for review in current time. On the live site this new copy needs to show on a set time and date in the future.
For example:
preview.webaddress.co.uk (staging area) to show new copy.
www.webaddress.co.uk (live area) not to show until said date and time.
This is how it looks at present:
<?php
date_default_timezone_set("europe/london");
$current_date = intval(date("YmdHi"));
if(201701011000 < $current_date ) {
?>
<?php include('new_copy_for_new_time_to_add.php');?>
<?php } ?>
<?php include('existing_copy_which_stays.php');?>

I've made amendments to above code and added more info to help.

i don't know your problem.
but you need
delete final line
?>

Related

wordpress get user registered date in pure php,returns 1970-01-01

I am trying to get user registered date in php file,always return 1970-01-01?(I need date format like as:2016-06-14)
What I know is php need to include wordpress themes first,then get user info
<?php
define('WP_USE_THEMES', false);
require('wp-blog-header.php');
?>
<?php
$reg_date=date("Y-m-d ", strtotime($registered));
echo $reg_date;
?>
I think it might need codes to connect current user,
get_currentuserinfo();
...
So any one know how to get it?
Really old question, but if someone finds this error like me. Be sure that your string format date is not an empty string.
date("Y-m-d", strtotime("")) == "1970-01-01"

PHP Timezone adjust as per user

Im creating a UI where users get to choose their timezone. My backend is PHP and I know you can do it a few ways but I had questions about all of them and which way is the best:
http://ca2.php.net/date_default_timezone_set
//where America/Los_Angeles will be changed based on user
date_default_timezone_set('America/Los_Angeles');
Does this change the server timezone for ever user or just what is being exported? is there any performance issues with this?
What if it was a function like this...?
date_default_timezone_set('America/Los_Angeles'); -08:00 set in php.ini
$date = "2014-01-01 12:00:00";
$new_time_zone = "America/New_York" // -05:00 = +3 hours difference
function adjust_time_zone($string, $zone){
$datetime = new DateTime($string);
$new_zone = new DateTimeZone($zone);
$datetime->setTimezone($new_zone);
return ($datetime->format('Y-m-d H:i:s'));
}
adjust_time_zone($date, $new_time_zone);
Results(i tested this):
2014-01-01 15:00:00 //3 hours ahead.
in this format everything stamped in the system would be on LA time and exported would be the change...
I know there is allot of these Timezone threads but there seems to be allot of "i do it like this" and not any solid "this way blows my socks off="
Thanks for any help you can give!
I store everything in the db as a datetime and UTC timezone.
I store the user's timezone in the db ($timezone).
So when a time ($time) is taken from the db, I run it through the following function:
function changetimefromUTC($time, $timezone) {
$changetime = new DateTime($time, new DateTimeZone('UTC'));
$changetime->setTimezone(new DateTimeZone($timezone));
return $changetime->format('m/d/y h:i a');
}
This returns the time in the user's timezone, formatted.
edit: you can see the db table of timezones in this thread:
Generating a drop down list of timezones with PHP
Well it looks like what you have should work for you. This is an alternate method, although not necessarily better. You can just use PHP's mktime and pass the hour offset to it.
<?php
print "<br>Date: ".date("Y-m-d H:i:s");
// DEFINE AN OFFSET
$offset = -5; // TIMEZONE OFFSET: '-5' FOR NEW YORK
// ADD THE OFFSET TO THE CURRENT TIME
print "<br>Date Offset: ".$date_offset = date("Y-m-d H:i:s", mktime(date("H") + $offset, date("i"), date("s"), date("m"), date("d"), date("Y")));
I found a great solution to the problem - and to adapt the date and time settings on my page according to the users settings.
Ready build js code
Some developers that had the exact same problem build a javascript solution to the code. You don't need to know javascript as long as you follow these instructions.
Go to https://bitbucket.org/pellepim/jstimezonedetect/src/default/
Read through the readme file if you want to
Either download the script as an NPM package or download it manually by pressing "downloads" in the left menu.
If you pressed downloads to manually download the script press "Download repository" to get the files.
Create a js folder - named "js" in your project. (optional)
Unzip the jstimezonedetect files into the js folder. I chose to name the unziped file "jstimezonedetect".
To reach the files above my current directory is /js/jstimezonedetect/.
In your header code (important it is in header since the time-zone will affect the full code on the rest of your page unless you run a function.
Enter the following code to your php file:
echo '
<script src="js/jstimezonedetect/dist/jstz.min.js"></script>
<script>
document.cookie = "usertimezone="+jstz.determine().name();
</script>
';
$settimezone = $_COOKIE['usertimezone'];
date_default_timezone_set($settimezone);
The first line of script will import the jstz.min.js file from the importat jstimezonedetect project.
Second line will create a cookie (you can later delete the cookie if you want to).
In the third line I get the cookie into the php script and then I use PHP date_default_timezone_set();
(https://www.php.net/manual/en/function.date-default-timezone-set.php)
to set the timezone.
Worked perfectly for me. :) Ask if something is unclear.

change global variable on given date

I guess, this is a very simple question. I want my wordpress theme to automatically change some global variables on a given date.
My Theme changes color and some other things every two months. From now on, I want to type in the needed variables before that date and let Wordpress do the changes. I could do that from within the loop, so that the first person who enters the site on that date initiates the change. But that would mean extra code, everytime the loop is called. Is it possible to perform that task automatically?
I haven't used WP in ages, but I think this will work, change time_to_change_theme to the forward date you want. I have no idea where to put this, I am sure you will work it out though.
<?php
$time_to_change_theme = strtotime("2012-12-31 12:12:12"); // the time in the future you want to change the theme
$time_now = strtotime(now);
if($time_to_change_theme > $time_now)
{
echo "Use current theme";
}else{
echo "Change theme";
update_option('current_theme', '[theme name]'); // this should update the current theme
}
?>
Uses code from this post by harmen

Implementing trial period for local PHP application

I recently finished my PHP app and i would like to know if there's a possibility to have a kind of trial period. So that when that period is reached an error appears on the screen.
Thanks for your time and answers. I most appreciate them.
You can encrypt your sources using Zend Encoder or Ioncube, or obfuscate them.
But the fact is that everything from above can be patched. So if target user is experienced enough - they could remove your trial period checks.
Yes you can: I did the same when the need arose some time ago. Its simple and you can improve it.
Follow this algorithm;
Get today's date
Set the trial period
Add the trial period to today( as start date) to get the expiry date
Check to see if the above code have been executed before
If not then send startdate and `expierydate` into a table in mysql
Else get the start date and compare with today
If it matches displ
Here is the code.
function timebomb(){
$today = date("d-M-Y",time());
$trialPeriod = 1;
$startDate = date("d-M-Y", time());
$getExpiryDate = strtotime('+'.$trialPeriod."days", strtotime($startDate));
$expiryDate = date("d-M-Y", $getExpiryDate);
$checkStatus = mysql_num_rows(mysql_query("SELECT * FROM timebomb"));
if($checkStatus == 0){
mysql_query("INSERT INTO timebomb(StartDate,ExpiryDate) values
('$startDate','$expiryDate')") or die(mysql_error());
}else{
$getPeriod = mysql_query("SELECT * FROM timebomb");
WHILE($period = mysql_fetch_object($getPeriod)){
$endOfTrial = $period->ExpiryDate;
}
IF($endOfTrial == $today){`enter code here`
echo
<center><font size='5' color='red'>
PLEASE YOUR TRIAL PERIOD IS OVER.
IF YOU ENJOYED USING THIS PRODUCT, <br/>
CONTACT ALBERT (0205173224) FOR THE FULL VERSION.
THANK YOU."
;
exit();
}
}
}
timebomb();
One solution can be the using some compiled (or byte compiled ) form of your application instead of source files. checkout this links.
http://php.net/manual/en/book.bcompiler.php, http://www.phpcompiler.org
Follow below steps for fully secured trial period in php projects...
1. create a table in your database for trial period
ex. trial(id,reg_date,days_of_trial).
2. In your index page or login page whatever it is,add code logic as below
i.get 'reg_date','days_of_trial' from 'trial' table.<br/>
ii.get today's date.<br/>
iii.calculate difference using as $dDiff= date_diff($reg_date,$today); <br/>
iv.now compare difference with 'days_of_trial'.<br/><br/>
ex. <br/>
if($dDiff->days>=$row['days_of_trial'])<br/>
{ <br/>
v.update trial table set days_of_trial=0.<br/><br/>
//redirect to expired page..<br/><br/>
ex.
header("Location:http://localhost/project_dir/expired/index.php");}.
3. in expired/index.php file remove your projects main directories as..<br/><br/>
ex.
<br/>
if(file_exists("../folder1"))<br/>
rmdir("../folder1");
redirect to expired page wherein you can display "Trial expired message"<br/>
ex.
<br/>
header("Location: http://localhost/proj_dir/expired/expired.html");<br/><br/>
4. now replace your original home page code to redirect to expired page..
ex.
$file = fopen("../index.php","w");<br/>
fwrite($file,"<?php header('Location: http://localhost/proj_dir/expired/');?>");<br/>
fclose($file);<br/>
Don't forget to take backups of project directory and database..
Your php project is fully secured as in trial period .....

Display content based on date with PHP

I'm putting together a contest site built on Wordpress. Legally, the contest has to start at midnight. To avoid being up at midnight to set up the content, i'd like to build some PHP logic to show everything after the start date, and before then display some basic HTML for a splash page. I'm a TOTAL programming newb, but i'm trying to work the problem out, here's what I have so far:
<?php
// The current date
$date = date('Y, j, m');
// Static contest start date
$contestStart = ('2012, 02, 03');
// If current date is after contest start
if ($date > $contestStart) {
//contest content?
}
else {
// splash content?
}
?>
I feel like there is smarter ways to do this. My site is fairly large... wrapping the whole thing in an if statement seems ridiculous. Maybe a redirect to a different page altogether based on the date?
Any help is appreciated.
You will want to modify your WordPress theme so that the changes can be site-wide.
Log in to your WordPress installation.
Navigate to Appearance > Editor
In the Templates section, go to header.php
At the very beginning of header.php, insert your code :
<?php
$date = time();
$contestStart = strtotime('2012-02-03 00:00:00');
if ($date < $contestStart) {
?>
<html>
Insert your whole splash page here.
</html>
<?php
exit;
}
?>
//The normal template code should be below here.
Don't forget to click the "Update File" button on WordPress when you're done; and like the others said, make sure that your specified time is synced with whatever timezone the server is in.
I suppose technically your code's logic would work but yes, there are much better ways of doing this. However, for your purpose we will go simple.
You should be comparing against a timestamp and not a string. Try this:
<?php
// The current date
$date = time();
// Static contest start date
$contestStart = strtotime('2012-02-03 00:00:00');
// If current date is after contest start
if ($date > $contestStart) {
//contest content?
}
else {
// splash content?
}
?>
You can put the splash and content components in separate .php files, and simply include() then within the conditionals.
if ($date > $contestStart) {
include("SECRETDIR/contest.php");
}
else {
include("SECRETDIR/splash.php");
}
You'll want to set up an .htaccess so that people cannot point their browsers towards secretdir and directly access contest.php
You don't have to wrap the content, you can have something like:
<?php
if ( time() < strtotime('2012-02-03 00:00:00') ) {
echo "not yet!";
exit;
}
//code here wont be executed
Be careful of timezones! Your server might be in a different timezone than your content.
// setup the start date (in a nice legible form) using any
// of the formats found on the following page:
// http://www.php.net/manual/en/datetime.formats.date.php
$contestStart = "February 1, 2012";
// check if current time is before the specified date
if (time() < strftime($contestStart)){
// Display splash screen
}
// date has already passed
else {
// Display page
}

Categories