Support

Account

Home Forums Front-end Issues Manipulating datepicker data

Solving

Manipulating datepicker data

  • I have two possible roles for date picker. The site I’m building offers courses running on specific dates.

    1. I need to use the yyyy-mm-dd data to order the courses in date order course list using Sorting Woocommerce plugin

    2. I also need to separate the values from the datepicker D, dd, mm, yy so I can display the date on the course page.

    I’m currently having to enter the date twice once as an attribute in woocommerce for sorting and ones as a three custom meta boxes one for date, one of month and one for year. This seems very cumbersome so hoping that I can separate out the values from datepicker to use accordingly.

    Does this make sense.

    Pete

  • Hi @petehawk

    Perhaps you could take a look at the docs page to see how you can use the datepicker in different formats. There are a few different tactics to extract the date attributes and re-format them.

    Good luck

    Thanks
    E

  • I followed istructions to manipulate the results here
    http://www.advancedcustomfields.com/resources/field-types/date-picker/
    but I didn’t yet understand how to change the language.

    here my code:

    <?php $date = get_field('name-field');
    $y = substr($date, 0, 4);
    $m = substr($date, 4, 2);
    $d = substr($date, 6, 2);
    $time = strtotime("{$d}-{$m}-{$y}");?>
    <?php echo date('l D M Y', $time); ?>

    It work but I need it in other language and in forums I didn’t find the relative solution.
    Help, please.

  • Hi @bildworter

    Please search this forum as there are a few discussions regarding translating a date picker value.

    Thanks
    E

  • Elliot

    Ok have found a bit of time to try and get my head round extracting elements of the date picker value.

    However this code

    <?php 
     
    	$date = get_field('course_date');
    	// $date = 19881123 (23/11/1988)
     
    	// extract Y,M,D
    	$y = substr($date, 0, 4);
    	$m = substr($date, 4, 2);
    	$d = substr($date, 6, 2);
     
    	// create UNIX
    	$time = strtotime("{$d}-{$m}-{$y}");
     
    	// format date (23/11/1988)
    	 
    	?>
    	<div class="calendar">
    	<p class="calendarDate"><span class="dow"><?php echo date('D' ); ?></span>
    		<span class="day"><?php echo date('d'); ?> <br /><?php echo date('M'); ?></span>
    		<span class="year"><?php echo date('Y'); ?></span>
    		</p>
    		</div>

    Outputs today’s date and not the date entered via Date Picker whose Field name is course_date.

    If I used the code on http://www.advancedcustomfields.com/resources/field-types/date-picker/ I get the 1st Jan 1970 outputted.

    A steer in the right direction would be a lifesaver. php version is 5.2.17

    Website: http://tideswellschooloffood.co.uk

  • Elliott

    How often does this happen…

    Solution found

    <?php 
     
    		$date = get_field('course_date');
    		// $date = WED2013NOV6 (23/11/1988)
     
    		// extract Y,M,D
    		$D = substr($date, 0, 3);
    		$y = substr($date, 3, 4);
    		$m = substr($date, 7, 3);
    		$d = substr($date, 10, 2);
    		
     
    		// create UNIX
    		$time = strtotime("{$d}-{$m}-{$y}");
     
    		// format date (23/11/1988)
    	 
    		?>
    		<div class="calendar">
    		<p class="calendarDate"><span class="dow"><?php echo $D; ?></span>
    			<span class="day"><?php echo $d; ?> <br /><?php echo $m; ?></span>
    			<span class="year"><?php echo $y; ?></span>
    			</p>
    			</div>

    Outputs as I wish and all hunky-dory… just a final one… with the date format now saved as WED2013NOV6 … can I wort the courses into a dater ordered list? Or are you going to tell me to read the docs 🙂

    Pete

  • Hi @petehawk

    Just to confirm, the issue was that you are not using the ‘yymmdd’ format for the save format setting in the field?

    For sorting to work in the get_posts function, you will need to use this yymmdd format to allow the database to order based on an integer number.

    Sorry if that’s not what you wanted to hear.

    Thanks
    E

  • Elliot

    No I haven’t used yymmdd as we need the day of the week to show too. Can I, using the entered data (DyyMd), “reconstruct” it as yymmdd and use that in the get_posts function?

    Code now

    <?php 
     
    		$date = get_field('course_date');
    		// $date = WED2013NOV6 (23/11/1988)
     
    		// extract Y,M,D
    		$D = substr($date, 0, 3);
    		$y = substr($date, 3, 4);
    		$m = substr($date, 7, 3);
    		$d = substr($date, 10, 2);
    		
     
    		// create UNIX
    		$time = strtotime("{$d}-{$m}-{$y}");
     
    		// format date (23/11/1988)
    	 
    		?>
    		<div class="calendar">
    		<p class="calendarDate"><span class="dow"><?php echo $D; ?></span>
    			<span class="day"><?php echo $d; ?> <br /><?php echo $m; ?></span>
    			<span class="year"><?php echo $y; ?></span>
    			</p>
    			</div>

    Cheers

    Pete

  • Hi @petehawk

    Can you elaborate more on what your mean by:
    Can I, using the entered data (DyyMd), “reconstruct” it as yymmdd and use that in the get_posts function?

    Basically, for the get_posts function to query or sort based on a date picker value, it must be a numeric value in the JS date_format of ‘yymmdd’. This may not be the answer you want to here, but it is the reality.

    If you could change the format and re-save your posts, the the query will work.
    There is a way to get the day of week from the format yymmdd.

    If you have a look at the docs:
    http://www.advancedcustomfields.com/resources/field-types/date-picker/

    You will see that an example uses a create from format function and contains a modify function for the output. You can read up on the PHP docs for modify, but I know there is a string output which will get the day of week.

    Hope that helps.

    Thanks
    E

Viewing 9 posts - 1 through 9 (of 9 total)

The topic ‘Manipulating datepicker data’ is closed to new replies.