How to add class in a string in PHP - php

I would like to put class inside this string :
<div class="date-custom-page">
<?php
$date = get_field('jour_de_levenement', false, false);
$time = strtotime($date);
?>
<?php $dateformatstring = "<p class='date-event'>j</p><p class='month'>F Y</p>"; ?>
<?php $unixtimestamp = strtotime(get_field('jour_de_levenement', false, false));
echo date_i18n($dateformatstring, $unixtimestamp); ?>
I have tried this but only the p is taken, not the class.
Can someone help me ?
Thanks a lot !

Just use this :
$dateformatstring = "<p class='date-event'>j</p><p class='month'>F Y</p>";

Related

displaying icon if date created and local date are the same php

<?php
date_default_timezone_set('America/New York');
$servermonth = date('m/d/Y ', time());
$createdate = new DateTime($row['createdon']);
$newdateformat =$createdate->format('m/d/Y') ;
// echo $newdateformat;
// echo $servermonth;
if (servermonth == newdateformat) {
?>
<span class="label label-default">New</span>
<?php
}
?>
basically it should show the "New " icon when the date is created today. they are both echo-ing the same date but the span is not showing up
Try using this:
if (strtotime(servermonth) === strtotime(newdateformat)) {
echo '<span class="label label-default">New</span>';
}
<?php
date_default_timezone_set('America/New York');
$servermonth = date('m/d/Y ', time());
$createdate = new DateTime($row['createdon']);
$newdateformat =$createdate->format('m/d/Y') ;
// echo $newdateformat;
// echo $servermonth;
if (strtotime($servermonth) == strtotime($newdateformat)) { ?>
<span class="label label-default">New</span>
<?php } ?>
It should be:
<?php
date_default_timezone_set('America/New York');
$servermonth = date('m/d/Y ', time());
$createdate = new DateTime($row['createdon']);
$newdateformat = $createdate->format('m/d/Y') ;
// echo $newdateformat;
// echo $servermonth;
if (strtotime($sservermonth) == strtotime($newdateformat)) {
echo '<span class="label label-default">New</span>'
}
?>

FullCalendar - How to show event based upon comma separated dates from database(mysql) table row on calendar

I am trying to show multiple events based upon dates from the same row of the table in the database separated by commas as show in picture:
Image from the database table
This is the my php/wordpress code that fetch the result from database:
$my_query = new WP_Query($args);
if ($my_query->have_posts()) {
$i = 0;
while ($my_query->have_posts()) : $my_query->the_post();
?>
<?php
$title = get_the_title();
//$date = explode(',',get_post_meta($post->ID, 'trainee_date_pick', true));
$date = get_post_meta($post->ID, 'trainee_date_pick', true);
//$dateArray = explode(',', $date);
$mealdes = nl2br(get_post_meta($post->ID, 'meal_plan_des', true));
$url = get_post_meta($post->ID, 'youtube_videos', true);
$url_two = get_post_meta($post->ID, 'youtube_videos_sec', true);
$meal_lable = get_post_meta($post->ID, 'meal_plan_label', true);
$array_cal[$i]["title"] = $title;
$array_cal[$i]["start"] = $date;
$array_cal[$i]["url"] = $url;
$array_cal[$i]["urltwo"] = $url_two;
$array_cal[$i]["description"] = $mealdes;
$array_cal[$i]["meallable"] = do_shortcode("[nutrition-label id=" . $meal_lable . "]");
$i++;
?>
<?php
endwhile;
}
echo json_encode($array_cal);
?>
After searching I try it doing myself, above code return the json like this but didn't show anything on the calendar but when there is single date it showed up.
[{"title":"Full Body","start":"2015-12-09, 2015-12-31","url":"","urltwo":"","description":"Deadlift 5x5
\r\nPause Squats 3x8
\r\nBench Press 3x8
\r\nClose Grip Bench 3x12
\r\nAlternating Curls 3x12
\r\nDips 3xMax
\r\nJump Rope 20 minutes
\r\n","meallable":""}]
And this is the jQuery code where I read json array:
<script type="text/javascript">
jQuery(document).ready(function () {
jQuery('#calendar').fullCalendar({
editable: true,
droppable: true,
eventLimit: true,
events: "/calendar-events/",
eventRender: function (event, element) {
element.attr('href', 'javascript:void(0);');
element.click(function () {
jQuery("#startTime").html(moment(event.start).format('DD-MM-YYYY'));
jQuery("#eventInfo").html(event.description);
jQuery("#eventLink").attr('href', event.url);
jQuery("#eventLink2").attr('href', event.urltwo);
jQuery("#meal_label").html(event.meallable);
// jQuery("#labeledit").attr('href',event.editlab);
// jQuery('#eventedit').attr('href',event.eventedit);
jQuery("#eventContent").dialog({modal: true, title: event.title, width: 350});
});
}
});
});
</script>
I really need help please somebody come up the solution.
Okay, now that I understand your problem... It's just a matter of going through the list of dates for each event that may have multiple. For this, going to un-comment one of your lines, and add two more. It will look something like this:
$my_query = new WP_Query($args);
if ($my_query->have_posts()) {
$i = 0;
while ($my_query->have_posts()) : $my_query->the_post();
?>
<?php
$title = get_the_title();
//$date = explode(',',get_post_meta($post->ID, 'trainee_date_pick', true));
$date = get_post_meta($post->ID, 'trainee_date_pick', true);
$dateArray = explode(',', $date);
$mealdes = nl2br(get_post_meta($post->ID, 'meal_plan_des', true));
$url = get_post_meta($post->ID, 'youtube_videos', true);
$url_two = get_post_meta($post->ID, 'youtube_videos_sec', true);
$meal_lable = get_post_meta($post->ID, 'meal_plan_label', true);
foreach($dateArray as $date) {
$array_cal[$i]["title"] = $title;
$array_cal[$i]["start"] = trim($date);
$array_cal[$i]["url"] = $url;
$array_cal[$i]["urltwo"] = $url_two;
$array_cal[$i]["description"] = $mealdes;
$array_cal[$i]["meallable"] = do_shortcode("[nutrition-label id=" . $meal_lable . "]");
$i++;
}
?>
<?php
endwhile;
}
echo json_encode($array_cal);
?>

Displaying same time passed in while through time_passed function

I'm doing a while() and in it I use a function to display a prettier "time since posted", but for some reason, all of the results end up in the same "time passed" (the last result) when echoing it out
only the time will become the same, not the other content. Here's my code, trimmed down a bit:
<?php
$result = $db->query("SELECT * FROM $dbboardname WHERE replyto = '' AND hidden = '' ORDER BY bump ASC LIMIT $pagenum, 20") OR die($db->error);
while($rowaaa = $result->fetch_assoc()){
$id = $rowaaa['id'];
$textcontent = $rowaaa['textcontent'];
$imageurl = $rowaaa['imageurl'];
$name = $rowaaa['nameyes'];
$timestamps = $rowaaa['timestamp'];
$timestamps = time_passed($timestamps);
$tripcode = $rowaaa['tripcode'];
?>
<div class="post-index" data-postid="<?php echo($id); ?>" name="<?php echo($id); ?>" id="<?php echo($id); ?>">
<div class="image-container-index">
<?php if(!empty($imageurl)){ ?>
<img src="<?php echo($imageurl); ?>" alt="image <?php echo($id); ?>" />
<?php }else{
?>
<img src="/images/noimage.png" alt="no image" />
<?php
}
?>
</div>
<div class="post-info">
<span class="post-info-left">
#<?php echo($id); ?> by <?php if (!empty($name)){
echo(substr($name, 0, 8));
}else{
echo("norseman");
}
?>
</span>
<span class="post-info-right">
<?php echo($timestamps); ?>
</span>
<div class="clear"></div>
</div>
<div class="post-preview">
<?php echo(substr($textcontent, 0, 150)); ?>
</div>
</div>
<?php
}
?>
<div class="clear"></div>
And here is the time_passed() function:
function time_passed($datetime, $full = false) {
$now = new DateTime;
$ago = new DateTime($datetime);
$diff = $now->diff($ago);
$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;
$string = array(
'y' => 'year',
'm' => 'month',
'w' => 'week',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second',
);
foreach ($string as $k => &$v) {
if ($diff->$k) {
$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
} else {
unset($string[$k]);
}
}
if (!$full) $string = array_slice($string, 0, 1);
return $string ? implode(', ', $string) . ' ago' : 'just now';
}
So my guess is that for every new "while", it edits all of the timestamps. I just don't know how to fix this, considering the string is inside an while() statement.
EDIT:
I noticed my timestamp updated because I altered the bump row on every post each time a new thread was posted, this way all the timestamps were updated. I fixed the timestamp row now.
It looks like your time_passed routine is expecting a datetime - not a timestamp, perhaps that is the error.
function time_passed($datetime, $full = false) {
Try to change this line:
$ago = new DateTime($datetime);
to this:
$ago = new DateTime(date('r',$datetime));
I hope that helps.
Let's try this again ... since they are all coming out the same, it sounds like the value you are passing to the function is wrong then - try putting a a test around the timestamp assignment:
$timestamps = $rowaaa['timestamp'];
echo "Made |$timestamps| from |{$rowaaa['timestamp']}<br/>\n";
$timestamps = time_passed($timestamps);
echo "Now timestamps is |$timestamps| from time_passed() function<br/>\n";
If they are all the same, the problem is in your data.
You can also show the entire content of the array by doing this:
echo "Full Details<pre>\n".print_r($rowaaa,1)."\n</pre>\n";

ICS URL outputting 5 hours behind

I'll start off by saying that I am not a PHP developer. So I had another developer create a plugin that takes the start and end time from the Wordpress plugin CalendarizeIt and outputs that into a formatted ICS url. The problem is that the url is outputting a start and end time 5 hours earlier than it should.
Here's the plugin PHP:
<?php
// Original script from http://jamiebicknell.tumblr.com/post/413492676/ics-generator-php-class#_=_
// Modified by Sean Carruthers
$start = $_GET['start'];
$end = $_GET['end'];
$name = $_GET['name'];
$description = $_GET['description'];
$location = $_GET['location'];
$uid = "kaneko" . strtotime("now");
$data = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nMETHOD:PUBLISH\r\nBEGIN:VEVENT\r\nDTSTART:".date("Ymd\THis\Z",$start)."\r\nDTEND:".date("Ymd\THis\Z",$end)."\r\nLOCATION:".$location."\r\nTRANSP: OPAQUE\r\nSEQUENCE:0\r\nUID:".$uid."\r\nDTSTAMP:".date("Ymd\THis\Z")."\r\nSUMMARY:".$name."\r\nDESCRIPTION:".$description."\r\nPRIORITY:1\r\nCLASS:PUBLIC\r\nEND:VEVENT\r\nEND:VCALENDAR";
header("Content-type:text/calendar; charset=utf-8");
header('Content-Disposition: inline; filename="'.$name.'.ics"');
echo $data;
?>
Then in the Wordpress theme file:
<?php
$start_date = get_post_meta($post->ID, "fc_start", true);
$c_start_date = date("M j, Y",strtotime($start_date));
$end_date = get_post_meta($post->ID, "fc_end", true);
$c_end_date = date("M j, Y",strtotime($end_date));
$start_time = get_post_meta($post->ID, "fc_start_time", true);
$end_time = get_post_meta($post->ID, "fc_end_time", true);
$ics_args['start'] = strtotime($start_date . " " . $start_time);
$ics_args['end'] = strtotime($end_date . " " . $end_time);
$ics_args['name'] = get_the_title();
$ics_args['description'] = get_the_content();
$ics_args['location'] = get_post_meta($post->ID, "location", true);
$ics_url = plugins_url('kaneko/calendarize-it-mods/ics_event.php') . "?";
foreach($ics_args as $key => $value) {
$ics_url .= "$key=$value&";
}
if($c_start_date == $c_end_date) {
echo $c_start_date;
} else {
echo $c_start_date . " - <br />" . $c_end_date;
}
?>
And finally the $ics_url variable is getting echoed in an anchor tag like so:
<a href="<?php echo $ics_url; ?>">
I've tried changing the strtotime in the $uid variable of the plugin to something like +5 hours but it does not seem to work.
Any help would be greatly appreciated.
Oh and I also played around with changing the timezone in the Wordpress settings. It is currently set to UTC-5, so I thought changing it to like Chicago would help but it did not either.
In the line below add X-WR-TIMEZONE:America/New_York
$data = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nMETHOD:PUBLISH\r\nX-WR-TIMEZONE:America/New_York\r\n
UPDATE: i have updated the whole line of code, its using DTSTART;VALUE=DATE: with out the Z and the same for DTEND
$data = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nMETHOD:PUBLISH\r\nX-WR-TIMEZONE:America/New_York\r\nBEGIN:VEVENT\r\nDTSTART;VALUE=DATE:".date("Ymd\THis",$start)."\r\nDTEND;VALUE=DATE:".date("Ymd\THis",$end)."\r\nLOCATION:".$location."\r\nTRANSP: OPAQUE\r\nSEQUENCE:0\r\nUID:".$uid."\r\nDTSTAMP:".date("Ymd\THis\Z")."\r\nSUMMARY:".$name."\r\nDESCRIPTION:".$description."\r\nPRIORITY:1\r\nCLASS:PUBLIC\r\nEND:VEVENT\r\nEND:VCALENDAR";

Display last date a post was updated on WordPress

I'm using some PHP to display the last time a blog post was updated on WordPress using the get_the_time and get_the_modified_time functions. However, I can't get the last modified time to display inline in the paragraph.
<?php
$x = get_the_time('U');
$m = get_the_modified_time('U');
if ($m != $x) {
$t = the_modified_time('F d, Y');
echo "<p class=\"lastupdated\">Updated on $t </p>";
}
?>
Here's a screenshot of the result:
the_modified_time prints the last modification time, so it prints it before you print your <p>.
Instead you'll want to use get_the_modified_time for setting $t, like so:
$t = get_the_modified_time('F d, Y');
It's probably because these functions don't return the result but echo it directly. Thus you must call them at the spot you need them to be.
<?php
$x = get_the_time('U');
$m = get_the_modified_time('U');
if ($m != $x) {
echo "<p class=\"lastupdated\">Updated on ".the_modified_time('F d, Y')."</p>";
}
?>
You can also display the last modified time of a post through this code by placing it anywhere in your loop.
Posted on <?php the_time('F jS, Y') ?>
<?php $u_time = get_the_time('U');
$u_modified_time = get_the_modified_time('U');
if($u_modified_time != $u_time) {
echo "and last modified on ";
the_modified_time('F jS, Y');
echo ". ";
} ?>
This is the complete code based on the above comments. Now it works and you just need to add this to your post template: Snippet (1)
<?php
$x = get_the_time('U');
$m = get_the_modified_time('U');
if ($m != $x) {
$t = get_the_modified_time('F d, Y');
echo "<p class=\"lastupdated\">Updated on $t </p>";
}
?>
For example, suppose that your post template is called content-post.php. Look for a part that looks like the below: Snippet (2)
// Post date
if ( in_array( 'post-date', $post_meta_top ) ) : ?>
<?php the_time( get_option( 'date_format' ) ); ?>
<?php endif;
Insert snippet (2) immediately before the closing tag of snippet (1) as follows:
<?php
// Post date
if ( in_array( 'post-date', $post_meta_top ) ) : ?>
<?php the_time( get_option( 'date_format' ) ); ?>
<?php
$x = get_the_time('U');
$m = get_the_modified_time('U');
if ($m != $x) {
$t = get_the_modified_time('F d, Y');
echo "<p class=\"lastupdated\">Updated on $t </p>";
}
?>
<?php endif;
Now, you will get the original post date and the updated post date. Special thanks go to #Sebastian Paaske Tørholm for his comment.

Categories