Jump to content

PHP returning D/M/Y and Time and I only want the D/M or D/M/Y


bwadamcik

Recommended Posts

Here is the code how would I go about getting only the D/M or D/M/Y

 

<?php $event_start_date = get_post_meta(get_the_ID(), 'event_start_date', true); $event_start_time = get_post_meta(get_the_ID(), 'event_start_time', true); $event_location = get_post_meta(get_the_ID(), 'event_location', true); if(!empty($event_start_date)) { ?>
 
                            <?php
 
global $redux_demo; 
if(isset($redux_demo['events-date-format'])) {
$time_format = $redux_demo['events-date-format'];
if($time_format == 1) {
 
?>
 
<?php $start_unix_time = strtotime($event_start_date); $start_date = date("m/d/Y",$start_unix_time); ?>
 
<?php } elseif($time_format == 2) { ?>
 
<?php $start_unix_time = strtotime($event_start_date); $start_date = date("d/m/Y",$start_unix_time); ?>
 
<?php } } else { ?>
 
<?php $start_unix_time = strtotime($event_start_date); $start_date = date("m/d/Y",$start_unix_time); ?>
 
<?php } ?>
 
<?php
 
global $redux_demo; 
if(isset($redux_demo['events-time-format'])) {
$time_format = $redux_demo['events-time-format'];
if($time_format == 1) {
 
?>
 
<?php $start_time = esc_attr($event_start_time); ?>
 
<?php } elseif($time_format == 2) { ?>
 
<?php $start_time = date("H:i", strtotime($event_start_time)); ?>
 
<?php } } else { ?>
 
<?php $start_time = esc_attr($event_start_time); ?>
 
<?php } ?>
 
       <span><?php echo esc_attr($start_date); ?> <?php echo esc_attr($start_time); ?></span>
 
                            <?php } ?>

Edited by cyberRobot
added code tags
Link to comment
Share on other sites

Nobody answered most likely because how much a mess the code was and a little bit more explanation needed as well.

 

Get date from where exactly? I see some there now.

<?php
$event_start_date = get_post_meta(get_the_ID(), 'event_start_date', true);
$event_start_time = get_post_meta(get_the_ID(), 'event_start_time', true);
$event_location   = get_post_meta(get_the_ID(), 'event_location', true);
if (!empty($event_start_date)) {
    global $redux_demo;
    if (isset($redux_demo['events-date-format'])) {
        $time_format = $redux_demo['events-date-format'];
        if ($time_format == 1) {
            
            $start_unix_time = strtotime($event_start_date);
            $start_date      = date("m/d/Y", $start_unix_time);
        } elseif ($time_format == 2) {
            
            $start_unix_time = strtotime($event_start_date);
            $start_date      = date("d/m/Y", $start_unix_time);
            
        }
    } else {
        
        $start_unix_time = strtotime($event_start_date);
        $start_date      = date("m/d/Y", $start_unix_time);
        
    }
    
    global $redux_demo;
    if (isset($redux_demo['events-time-format'])) {
        $time_format = $redux_demo['events-time-format'];
        if ($time_format == 1) {
            
            $start_time = esc_attr($event_start_time);
            
        } elseif ($time_format == 2) {
            
            $start_time = date("H:i", strtotime($event_start_time));
            
        }
    } else {
        
        $start_time = esc_attr($event_start_time);
        
    }
    
    echo "<span>" . esc_attr($start_date) . esc_attr($start_time) . "</span>";
    
}
?> 

I especially do not understand why this multiple if/elesif/else statement 1,2 and default are all exactly the same.

if ($time_format == 1) {
            
            $start_unix_time = strtotime($event_start_date);
            $start_date      = date("m/d/Y", $start_unix_time);
        } elseif ($time_format == 2) {
            
            $start_unix_time = strtotime($event_start_date);
            $start_date      = date("d/m/Y", $start_unix_time);
            
        }
    } else {
        
        $start_unix_time = strtotime($event_start_date);
        $start_date      = date("m/d/Y", $start_unix_time);
        
    }
Link to comment
Share on other sites

Hi QuickOldCar,

 

I have no idea myself and I apologize for my sad excuse for a question. I did see the redundancy. I have a website that has the code prewritten and after inspecting it I was looking to modify a section to be more user friendly instead of listing the items by number providing the specific date event instead. I am hoping to figure out how to just remove the time portion in this code so that I can just present the date.

 

How would it be best for me to present this question so it can be better understood?

 

Bang

Edited by bwadamcik
Link to comment
Share on other sites

Hi QuickOldCar,

 

I appreciate you giving me that information. I gave it a try and it didn't work.

 

<?php if($currentEvent <= 9) { ?>0<?php } ?><?php echo $currentEvent; ?>

 

this is what is currently there which reflects just a number. is there another method of writing it that you would suggest?

Link to comment
Share on other sites

Why are you going in and out of PHP tags on every line? That's completely unnecessary and makes your code very hard to follow.

 

I am hoping to figure out how to just remove the time portion in this code so that I can just present the date.

You can do that with date(). If you just have a date/time string instead of a UNIX timestamp, you can use strtotime() or the DateTime library to create one first.

Edited by scootstah
Link to comment
Share on other sites

 

<?php if($currentEvent <= 9) { ?>0<?php } ?><?php echo $currentEvent; ?>

 

this is what is currently there which reflects just a number. is there another method of writing it that you would suggest?

 

I don't see $currentEvent in your previous posted code.

 

 

I appreciate you giving me that information. I gave it a try and it didn't work.

 

I'm wondering how this didn't work.

<?php } ?>
 
       <span><?php echo esc_attr($start_date); ?></span>
 
                            <?php } ?>

Does your original code work with date and also time?

<?php } ?>
 
       <span><?php echo esc_attr($start_date); ?> <?php echo esc_attr($start_time); ?></span>
 
                            <?php } ?>
Link to comment
Share on other sites

That is my very confusion when I try and use this code above which I thought would work it does not reflect and throws an error for this line. 

 

The only way the error doesn't occur is when I put the whole code snippet I provided in and then it reflects the whole m/d/y time. I figured that I would need to look at this to fix it and add an additional format? 

 

Any thoughts?

 

global $redux_demo;
if (isset($redux_demo['events-time-format'])) {
$time_format = $redux_demo['events-time-format'];
if ($time_format == 1) {

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.