V easy – adding line to change time format in php
-
I really have searched for this (I am a newbie). I know the time formatting codes, just don’t know how to insert them in the event.single.php file
I currently have the line:
<?php echo $EM_Event->start_time;?> <?php echo __('to','crunchpress') ?> <?php echo $EM_Event->end_time;?>
and there is no other reference in that file to start_time and I’m not sure how to change the way it presents (currently 24 hour)
I tried adding the line
start_time = date('g,i,s',$EM_Event->start_time);
but that doesn’t work. Like I said, it’s too basic to be documented somewhere, hopefully very easy for you to enlighten me.
-
OK, I’m getting closer. If I do this:
<?php echo date("D F Y g i s a", $EM_Event->start_time);?>
Then I can format the output but the object is returning is returning just “19” (as 7pm) and date() is interpreting this as 19 seconds, so the above gives:
Thu January 1970 12 00 19 am
I don’t want to format it by subtracting 12 and simply outputting the number and calling it hours, or doing an if-greater-than-12-subtract-12 if I can avoid it. The alternative:
<?php echo $EM_Event->start_time;?>
gives:
19:00:00 October 12, 2015
Which is correct, but 24 hour format which I don’t want…..
for the 12 hour time, you can try h:i A
Thanks, but this line:
<?php echo date("h:i A", $EM_Event->start_time);?>
Returns:
12:00 AMBecause it is interpreting the start_time as 19 seconds – which rounds down to less than one minute, therefore 12am. The actual start time is 19.00 hours, and without the date() formatting it shows this correctly, I just want it in a 12 hour clock.
you can use $EM_Event->event_start_time instead.
That also returns 12:00 AM
both<?php echo date("h:i A", $EM_Event->start_time);?> <?php echo date("h:i A", $EM_Event->event_start_time);?>
Return 12:00 AM
and
<?php echo $EM_Event->start_time;?> <?php echo $EM_Event->event_start_time;?>
Return
19:00:00I think I need to be defining a time format somewhere else?
you can see this objects under classes/em-event.php
Thanks! I found the file classes/em-event.php
It does have
var $event_start_time;
and also the line
'event_start_time' => array( 'name'=>'start_time', 'type'=>'%s', 'null'=>true ),
All other (25 of) instances of ‘start_time’ seem to be logic or other uses – nothing that defines the time format. So if this file is the answer, what do I do with it?
Many thanks
Quentintime format is located in the admin option under events > settings > formatting > events > date/time section and you can see this in action under classes/em-event.php at around lines 1553-1565
OK, maybe this is harder than I thought. The admin option through the normal interface has time format of:
“g:i a
For use with the #_EVENTTIMES placeholder”I think that’s the part you’re referring to. If I thought it was as simple as using that placeholder I/we would have started there. The lines you point to in em-event.php are
case '#_EVENTTIMES': //get format of time to show if( !$this->event_all_day ){ $time_format = ( get_option('dbem_time_format') ) ? get_option('dbem_time_format'):get_option('time_format'); if($this->event_start_time != $this->event_end_time ){ $replace = date_i18n($time_format, $this->start). get_option('dbem_times_separator') . date_i18n($time_format, $this->end); }else{ $replace = date_i18n($time_format, $this->start); } }else{ $replace = get_option('dbem_event_all_day_message'); } break;
But I don’t see how this helps. dbem_time_format is referred to only here in this file and in the booking section.
I don’t want this to go on forever – please review my second post: the number being returned appears to be ’19’ – which is correct for the number of hours. But I want this formatted 12H clock. If I do the date formatting as shown it interprets the 19 as 19 seconds. I am a complete novice, but it seems to me that the answer is here somewhere.
This is not as simple as picking the right time format – it’s knowing WHERE to select it for this use of start_time. And we don’t know that yet.
Thanks for sticking with this.
Where are you trying all these variations? If it’s in a template, are you calling $EM_Event before your code?
The 1970 date you mentioned before is the standard date PHP uses when the actual date info is missing, so it could be the real date is not making it to your code.
I was wrong in the first post – I am editing not event.single.php, but single-event.php
This includes the following:
<div class="time-area"> <strong class="time"> <?php echo $EM_Event->event_start_time;?> <?php echo 'to'?> <?php echo $EM_Event->end_time;?> <!/strong> <!strong class="date"> <?php echo date(get_option('date_format'),$EM_Event->start);?> </strong> </div>
and it is this that is returning the frustrating format. Here is an example of an event that shows 24hr format
https://www.sustainablewellesley.com/events/plastics-what-to-do/As noted above various options are not working.
$EM_Event I understand is an object defined by Events Manager as standard. It is referred to earlier in that file. I really don’t know if this is relevant, but the following is included:global $post,$EM_Event; // Get Post Meta Elements detail $event_social = ''; $sidebar = ''; $left_sidebar = ''; $right_sidebar = ''; $event_thumbnail = ''; $video_url_type = ''; $select_slider_type = ''; $event_detail_xml = get_post_meta($post->ID, 'event_detail_xml', true); if($event_detail_xml <> ''){ $cp_event_xml = new DOMDocument (); $cp_event_xml->loadXML ( $event_detail_xml ); $event_social = find_xml_value($cp_event_xml->documentElement,'event_social'); $sidebar = find_xml_value($cp_event_xml->documentElement,'sidebar_event'); $left_sidebar = find_xml_value($cp_event_xml->documentElement,'left_sidebar_event'); $right_sidebar = find_xml_value($cp_event_xml->documentElement,'right_sidebar_event'); $event_thumbnail = find_xml_value($cp_event_xml->documentElement,'event_thumbnail'); $video_url_type = find_xml_value($cp_event_xml->documentElement,'video_url_type'); $select_slider_type = find_xml_value($cp_event_xml->documentElement,'select_slider_type'); }
The file that angelo refers to – classes/em-event.php – seems to define $EM_Event including the following:
/** * Event Object. This holds all the info pertaining to an event, including location and recurrence info. * An event object can be one of three "types" a recurring event, recurrence of a recurring event, or a single event. * The single event might be part of a set of recurring events, but if loaded by specific event id then any operations and saves are * specifically done on this event. However, if you edit the recurring group, any changes made to single events are overwritten. * * @author marcus */ class EM_Event extends EM_Object{ /* Field Names */ var $event_id; var $post_id; var $event_slug; var $event_owner; var $event_name; var $event_start_time; var $event_end_time;
Does this help at all??
In your first bit of code you have this line:
<?php echo date(get_option('date_format'),$EM_Event->start);?>
That’s taking the date formatting from your WordPress date format.
To get the format you want, either change the format under the Settings menu or change the line of code to something like this:
<?php echo date("H:i m F Y",$EM_Event->start);?>
This might help:
https://php.net/manual/en/function.date.php“To get the format you want, either change the format under the Settings menu or “
THIS DOES NOT WORK. I did do that before I even started this post (I said it was easy, but not THAT easy), then it was suggested once in the thread above already and I pointed out it didn’t work. Changes to those settings do not reach the page.
However, I think I worked out the problem
In a post further up I stated:
both
<?php echo date(“h:i A”, $EM_Event->start_time);?>
<?php echo date(“h:i A”, $EM_Event->event_start_time);?>
Return 12:00 AM (and 19 seconds)and
<?php echo $EM_Event->start_time;?>
<?php echo $EM_Event->event_start_time;?>
Return
19:00:00I think I need to be defining a time format somewhere else?
The answer is not defining it somewhere else, but with something else. My code was using ‘start_time’ when it seems should have been using ‘start’ (ditto end_time). Not sure why, and I don’t care, but maybe this matters to someone else/ the developers.
The following code works – provides 12h clock WITH the correct time (not 12 am and 19 seconds):
<div class="time-area"> <strong class="time"> <!/strong> <!strong class="date"> <?php echo date("h:i a ",$EM_Event->start);?> <?php echo 'to '?> <?php echo date("h:i a",$EM_Event->end);?> <br><?php echo date(" l F jS Y",$EM_Event->start);?> </strong> </div>
Produces:
7:00 pm to 9:00 pm
Saturday September 17th 2015Resolved above
- The topic ‘V easy – adding line to change time format in php’ is closed to new replies.