I'm going to create a "future"-blogg (sort of a sci-fi-adventure in blog form) and want to display all dates +100 years. For instance a post published 2012-05-17 should display the date 2112-05-17.
First I thought I could just easily set the date to 2112-05-17, but it seems that wordpress can't handle dates higher than 2049.
So my next idea is to modify how the dates are displayed. I was thinking of modifying get_the_date() in general-template.php, and make it return the later date.
But here my skills are not enough. I don't know anything about how to work with date values in php.
get_the_date() looks like this:
function get_the_date( $d = '' ) {
global $post;
$the_date = '';
if ( '' == $d )
$the_date .= mysql2date(get_option('date_format'), $post->post_date);
else
$the_date .= mysql2date($d, $post->post_date);
return apply_filters('get_the_date', $the_date, $d);
}
Any ideas on how to modify it? So it adds 100 years to the date before returning it?
Any input would be appriciated :)
Looks like you might need to investigate date_modify and also strtotime
http://php.net/manual/en/datetime.modify.php
http://www.php.net/manual/en/function.strtotime.php
http://www.php.net/manual/en/datetime.add.php
Assuming your mysql dates are of the following format: YYYY-MM-DD
function add100yr( $date="2011-03-04" ) {
$timezone=date_timezone_get();
date_default_timezone_set($timezone);
list($year, $month, $day) = split(':', $date);
$timestamp=mktime(0,0,0, $month, $day, $year);
// 100 years, 365.25 days/yr, 24h/day, 60min/h, 60sec/min
$seconds = 100 * 365.25 * 24 * 60 * 60;
$newdate = date("Y-m-d", $timestamp+$seconds );
// $newdate is now formatted YYYY-mm-dd
}
Now you can:
function get_the_date( $d = '' ) {
global $post;
$the_date = '';
if ( '' == $d )
$the_date .= mysql2date(get_option('date_format'), add100yr($post->post_date));
else
$the_date .= mysql2date($d, add100yr($post->post_date));
return apply_filters('get_the_date', $the_date, $d);
}
Try a custom field: http://codex.wordpress.org/Custom_Fields
You will have to enter the +100 year date for each post, but then you're not going to be relying on php or functions to alter the current date.
WordPress provides the filter get_the_date that allows to modify the value before it is handled over to the theme or plugin.
This filter is used everytime get_the_date() is called.
add_filter( 'get_the_date', 'modify_get_the_date', 10, 3 );
function modify_get_the_date( $value, $format, $post ) {
$date = new DateTime( $post->post_date );
$date->modify( "+100 years" );
if ( $format == "" )
$format = get_option( "date_format" );
return( $date->format( $format ) );
}
This function takes the post_date from the post, adds the time and returns it according to the format given to get_the_date() or with the default format configured in the WordPress options.
Related
I'm trying to have 2 fields in my form become read only after a certain time & date.
I've cobbled together some code from a few places in an attempt to make this work but I'm a total novice so I can't identify where it's going wrong or why.
The fields I want conditionally read-only are 1 & 13 in the example below. I'm not even sure what the numbers 9 & 2 are for. Like I said, absolute beginner.
Adding Angel's time & date definitions from his answer, here's what I have so far...
$W1F1Start = "2020-10-16 12:42:00";
$now = date('Y-m-d H:i:s');
if(strtotime($now) > strtotime($W1F1Start)){
add_filter('frm_setup_new_fields_vars', 'frm_set_read_only_on_create', 9, 2);
add_filter('frm_setup_edit_fields_vars', 'frm_set_read_only_on_create', 9, 2);
function frm_set_read_only_on_create( $values, $field ){
if ( in_array( $field->id, array( 1,13 ) ) ) {
$values['read_only'] = 1;
}
return $values;
}
return $values;
}
If I get this to work, I eventually need to make up to 20 fields read-only at different times and dates in the same form.
My form is a Formidable form on wordpress if it makes a difference.
In their knowledge bank they have 2 bits of code that I assume I can put together somehow to do what I want, but I can't figure out how...
This one 'makes fields read only' based on a thing
add_filter('frm_setup_new_fields_vars', 'frm_set_read_only_on_create', 9, 2);
add_filter('frm_setup_edit_fields_vars', 'frm_set_read_only_on_create', 9, 2);
function frm_set_read_only_on_create( $values, $field ){
// If on the back-end, keep fields editable
if ( FrmAppHelper::is_admin() || current_user_can( 'administrator' ) ) {
return $values;
}
// If on front-end, make specific fields read-only
if ( in_array( $field->id, array( 554,555,556 ) ) ) {
$values['read_only'] = 1;
}
return $values;
}
And this one does something 'based on current date'
add_filter('frm_setup_new_fields_vars', 'remove_field_option_by_date', 30, 2);
add_filter('frm_setup_edit_fields_vars', 'remove_field_option_by_date', 30, 2);
function remove_field_option_by_date( $values, $field ) {
$today = time();
$close_date = strtotime ( "2020-06-20" ); // change 2020-06-20 to the date after which the option should be removed
if ($today id == 13677 ) { // change 13677 to your field id
$options_to_remove = array( 'Option 1' ); // change Option 1 to the value to remove
foreach ( $options_to_remove as $remove ) {
$option_key = array_search( $remove, $values['options'] );
if ( $option_key !== false ) {
unset( $values['options'][ $option_key ] );
}
}
}
return $values;
}
I need to 'make fields read only'-'based on a date'. But I'm at a loss as to how to put those two things together.
Hope this is all the information needed. Is this possible?
1st - in Wordpress "code snippets" go in functions.php, located inside the theme you're using, so can do this without a plugin. Now to compare 2 dates you can have something like this:
// Static W1F1 start date
$W1F1Start = new DateTime();
$W1F1Start->setTimestamp(1602840600);
$now = new DateTime();
if($now > $W1F1Start){
// do whatever
}
Or lets assume your variable $W1F1Start is a string "2020-10-16 09:30:00" (this is the value of formatted $W1F1Start->format('Y-m-d H:i:s')) and you want to compare that to what current time, then you can use strtotime(), which will convert your strings to timestamps:
$W1F1Start = "2020-10-16 09:30:00";
$now = date('Y-m-d H:i:s');
if(strtotime($now) > strtotime($W1F1Start)){
// do whatever
}
So this is your code final look:
add_filter('frm_setup_new_fields_vars', 'frm_set_read_only_on_create', 9, 2);
add_filter('frm_setup_edit_fields_vars', 'frm_set_read_only_on_create', 9, 2);
function frm_set_read_only_on_create( $values, $field ){
// Static W1F1 start date
$W1F1Start = new DateTime();
$W1F1Start->setTimestamp(1602840600);
// current time
$now = new DateTime();
$now->setTimezone(new DateTimeZone('UTC'));
// if cuurent time > W1F1Start
if ($now > $W1F1Start) {
if ( in_array($field->id, array(1,13))) {
$values['read_only'] = 1;
}
return $values;
}
return $values;
}
A site contains monthly data in a JSON format. This can be queried like so:
http://www.example.com?startdate=1-1-1985&enddate=31-1-1985
I want to be able to run a script that will obtain the specific data I am looking for, starting from today's month, then work backward until null data is returned. Each month's value needs to add up. So far, this is what I've got:
//Build base URL:
$userid=$_GET['userid'];
$startdate=/*Beginning of today's month*/;
$enddate=/*End of today's month*/;
$url='http://www.example.com?userid='.$userid.'&startdate='.$startdate.'&enddate='.$enddate;
//Set JSON variables:
$get=file_get_contents($url);
$json=json_decode($get);
//Set loop variables:
$value=0;
$month=0;
/*For each month from today backwards{
$number=$json->integer;
if($number!=null){
$value=$value+$number;
$month=$month+1;
}else{
break;
}
}
echo $value;
echo $month;
*/
The part I'm having problems with is the beginning of the fourth part. How do I run a loop that starts with the range of today's month, obtain $number, then repeat with the previous month, until it reaches a month that returns null?
You could use the DateTime object and it's associated methods ( specifically sub in this case ) to count backwards by subtracting 1month at a time. An array stores the months/dates and the url is constructed using the variable counter $i
Initially this has a maximum backwards range of 20years ( which I guessed would be more than enough ) but it's easy to change.
$df='Y-m-d';
$userid=$_GET['userid'];
$timezone=new DateTimeZone('Europe/London');
$interval=new DateInterval('P1M');
$ts=date( $df, strtotime( date('Y-m-1') ) );
$tf=date( $df, strtotime( date('Y-m-1'). ' - 20 years') );
$start=new DateTime( $ts, $timezone );
$end=new DateTime( $tf, $timezone );
$dates=array();
$i=0;
while( $start->sub( $interval ) > $end ){
$dates[]=$start->format( $df );
if( $i > 1 ){
$startdate=$dates[ $i - 1 ];
$enddate=$dates[ $i ];
$url='http://www.example.com?userid='.$userid.'&startdate='.$startdate.'&enddate='.$enddate;
echo $url,'<br />';
/* uncomment when happy with mechanism */
/*
$data=file_get_contents( $url );
if( $data ) ) {
$json=json_decode( $data );
#process.....
break;
}
*/
}
$i++;
}
A snippet of the output
http://www.example.com?userid=bobodaclown&startdate=2017-10-01&enddate=2017-09-01
http://www.example.com?userid=bobodaclown&startdate=2017-09-01&enddate=2017-08-01
http://www.example.com?userid=bobodaclown&startdate=2017-08-01&enddate=2017-07-01
http://www.example.com?userid=bobodaclown&startdate=2017-07-01&enddate=2017-06-01
http://www.example.com?userid=bobodaclown&startdate=2017-06-01&enddate=2017-05-01
http://www.example.com?userid=bobodaclown&startdate=2017-05-01&enddate=2017-04-01
http://www.example.com?userid=bobodaclown&startdate=2017-04-01&enddate=2017-03-01
By using strtotime() and mktime() functions you can while loop until you get Null results. The below code will print 5 urls for 5 months. Change while condition accordingly.
// define $i
$i=0;
$userid = '343';//$_GET['userid']; //dont forget to replace with userid
do{
$timestring = strtotime("now -$i month");
//get Month
$month = date('m',$timestring);
//get Year
$year = date('Y',$timestring);
// First date Month and Year
$startdate = date('d-m-Y', mktime(0, 0, 0, $month, 1, $year));
// Last date Month and Year
$enddate = date('d-m-Y', mktime(0, 0, 0, $month+1, 0,$year));
$url='http://www.example.com?userid='.$userid.'&startdate='.$startdate.'&enddate='.$enddate;
// commenting your code
//Set JSON variables:
//$get=file_get_contents($url);
//$json=json_decode($get);
// $number=$json->integer;
echo $url;
echo "\n";
$i++;
}while ($i!=5); // change while condition according to your requirement. while ($number!=null)
Out Put:
http://www.example.com?userid=343&startdate=01-12-2017&enddate=31-12-2017
http://www.example.com?userid=343&startdate=01-11-2017&enddate=30-11-2017
http://www.example.com?userid=343&startdate=01-10-2017&enddate=31-10-2017
http://www.example.com?userid=343&startdate=01-09-2017&enddate=30-09-2017
http://www.example.com?userid=343&startdate=01-08-2017&enddate=31-08-2017
I am using an ACF Field to allow a client to content manage a countdown to their next event I am using JS flip clock on the desktop version but as it isn't responsive, I decided to use date diff to echo out just the number of days for mobile.
The site is currently live at theindustrialproject.co.uk
The code I currently have is this:
<?php
$date1 = date_create(date());
$date2 = date_create(the_field('mobile_date'));
$diff = date_diff($date1,$date2);
$difference = $diff;
if ($difference < 0) { $difference = 0; }
echo '<span class="countdown-mobile">'. floor($difference/60/60/24)."</span><br />";
if ($difference == 1) { echo "<p>Day</p>"; }
else { echo "<p>Days</p>"; }
?>
but it always returns 0. For reference, I pulled the code from here
Without knowing what the function the_field('mobile_date') will return ( either a date or timestamp? ) you might need to alter that particular line below but you should be able to use the DateTime object and format the difference like this
$format='Y-m-d';
$timezone=new DateTimeZone('Europe/London');
/* We need 2 datetime objects - one for now the other for the future date */
$now=new DateTime( date( $format, strtotime('now') ), $timezone );
$target=new DateTime( the_field('mobile_date'), $timezone );
/* Format the difference in days */
$days = $now->diff( $target )->days;
echo "<span class='countdown-mobile'>{$days}</span><br />" . ( $days == 1 ? '<p>Day</p>' : '<p>Days</p>' );
I'm trying to add to this if statement another one that checks if post time is before a specific date (let's say 14th March 2014..).
<?php if (in_category('5')) {
echo 'something'; }
?>
I tried with this but it's echoed even on posts newer than that date
<?php
$date1 = "2014-03-14";
$date2 = $post->post_date;
if ( (in_category('5')) && ($date1 < $date2) ) {
echo 'something';
}
?>
I'd convert your dates to timestamps first to make them easier to compare.
global $post;
$compare_date = strtotime( "2014-03-14" );
$post_date = strtotime( $post->post_date );
if ( in_category(5) && ( $compare_date > $post_date ) ) ...
It looks like you have your operator the wrong way round as well. You wanted to check if posts are before the date. The post date would have to be smaller to be before. Also you're passing in category ID 5 as a string for some reason which I corrected in my code above.
Thanks to Nathan, his answer worked great for me.
global $post;
$compare_date = strtotime( "2016-07-14" );
$post_date = strtotime( $post->post_date );
if ( in_category(2) && ( $compare_date >= $post_date ) ) {
echo "On or before July 14."
} else {
echo "After July 14."
}
I have two variables stored in my database containing the following data:
$date_published = 2012-05-04 00:00:00; //Straight from DB datetime field
$advert_duration = 15;
I want to show an advert for 15 days from the date it was published. To do so I need to work out the time difference.
I have read various places online about calculating time difference and have come up with the below code
In my attempt to work out the equation I can't seem to calculate the differences between $now - the date today, the $date_published and the $advert_duration. I can't get the correct result:
function days_left($date_published, $advert_duration){
$date = new DateTime($date_published);
$now = new DateTime();
$days_elapsed = $date->diff($now)->format("%d");
$days_left = $advert_duration - $days_elapsed;
return $days_left;
}
function getDaysLeft( $date, $duration )
{
// create $date and modify it by $duration
$date = new DateTime( $date );
$date->modify( sprintf( '+%d days', $duration ) );
// calculate the difference
$now = new DateTime();
$daysElapsed = (int) $now->diff( $date )->format( '%a' );
// set to negative value, if modified $date is before $now
if( $date < $now )
{
$daysElapsed = $daysElapsed * -1;
}
return $daysElapsed;
}
var_dump(
getDaysLeft( '2012-05-04 00:00:00', 15 ),
getDaysLeft( '2012-07-04 00:00:00', 15 )
);
If you're fetching your ad from the database, you can simply use a date function to calculate this :
WHERE DATE_SUB(CURDATE(),INTERVAL 15 DAY) >= date
Or you can do this in PHP (you'll get an UNIX timestamp) :
$date = strtotime('+15 days', strtotime($date_published));