ISO Dates with custom fields

Hello,

I need to output a date that moment.js can understand. I have custom fields time and date. How do i combine these so that the result is this:

2017-04-16T16:00:00

Thanks

The full iso

<?php
$date = $page->date('Y-m-d');
$time = $page->time();
$isoDate = date('c', strtotime("$date $time"));
echo $isoDate;
?>

Or without the timezone stuff:

<?php
$date = $page->date('Y-m-d');
$time = $page->time();
$isoDate = date('Y-m-d\TH:i:s', strtotime("$date $time"));
echo $isoDate;
?>

Edit: You probably have to add the date format.

Check if time is empty:

<?php
$date = $page->date('Y-m-d');
$time = $page->time()->isNotEmpty()? $page->time(): '00:00';
$isoDate = date('Y-m-d\TH:i:s', strtotime("$date $time"));
echo $isoDate;
?>

Awesome! Thanks. That’s almost. I realised after posting that I only need the time on the end if the time custom field for the time has a value. I’ll see I can jig your code to do that… unless you beat me to it … :slight_smile:

Thanks for the tip.

Thanks for that. I’m not much of a back end coder but that looks to me like it checks if its empty OR 00:00? is that right? the question mark is an OR?

That’s a shorthand if statement using the ternary operator: if the field is not empty, it uses the value of the field, otherwise it sets the value to “00:00”-

I see, thats not quite right. I need it to not generate a time at all. Heres my full snippet. This is to initialise fullcallendar and gets populated from articles in an Events section

	<?php
	$events = $site->find('events')->children()->visible();
	?>
	<div id='calendar'></div>
	<script>
		$(document).ready(function() {

			$('#calendar').fullCalendar({
				header: {
					left: 'prev,next',
					center: 'title',
					right: 'today'
				},
				defaultDate: '<?php echo date('Y-m-d') ?>',
				navLinks: false, // can click day/week names to navigate views
				editable: false,
				eventLimit: true, // allow "more" link when too many events
				events: [
				<?php foreach($events as $event): ?>
					{
						<?php
						$startdate = $event->startdate();
						$starttime = $event->starttime()->isNotEmpty()? $page->time(): '00:00';
						$enddate = $event->enddate();
						$endtime = $event->endtime()->isNotEmpty()? $page->time(): '00:00';

						$startisoDate = date('Y-m-d\TH:i:s', strtotime("$startdate $starttime"));
						$endisoDate = date('Y-m-d\TH:i:s', strtotime("$enddate $endtime"));
						?>
						title: '<?php echo $event->title() ?>',
						url: '<?php echo $event->url() ?>',
						start: '<?php echo $startisoDate ?>',
						end: '<?php echo $endisoDate ?>',
					},
				<?php endforeach ?>
				]
			});

		});

	</script>

This results in:

	events: [
	{
	title: 'Launch Party',
	url: 'http://domain.dev/events/launch-party',
	start: '2017-04-25T00:00:00',
	end: '2017-04-28T00:00:00',
	},
	{
	title: 'Open Day',
	url: 'http://domain.dev/events/open-day',
	start: '2017-07-01T00:00:00',
	end: '2017-04-26T00:00:00',
	},
	]

However, if the time field is empty the start and end should read:

	start: '2017-07-01',

If you don’t want the ISO format if the time is empty, then you will have to use an if statement to create the start and end dates.

<?php
$date = $page->date('Y-m-d');
$time = $page->time()->isNotEmpty()? $page->time(): '';
if($time == "") {
  $isoDate = date('Y-m-d', strtotime("$date"));
} else {
  $isoDate = date('Y-m-d\TH:i:s', strtotime("$date $time"));
}

echo $isoDate;
?>

Brilliant! This did the trick… Thanks.

		<?php
	$events = $site->find('events')->children()->visible();
	?>
	<div id='calendar'></div>
	<script>
		$(document).ready(function() {

			$('#calendar').fullCalendar({
				header: {
					left: 'prev,next',
					center: 'title',
					right: 'today'
				},
				defaultDate: '<?php echo date('Y-m-d') ?>',
				navLinks: false, // can click day/week names to navigate views
				editable: false,
				eventLimit: true, // allow "more" link when too many events
				events: [
				<?php foreach($events as $event): ?>
					{
						<?php
						$startdate = $event->startdate();
						$starttime = $event->starttime()->isNotEmpty()? $event->starttime(): '';
						$enddate = $event->enddate();
						$endtime = $event->endtime()->isNotEmpty()? $event->starttime(): '';

						if($starttime == "") {
						$startisoDate = date('Y-m-d', strtotime("$startdate"));
						} else {
						$startisoDate = date('Y-m-d\TH:i:s', strtotime("$startdate $starttime"));
						}

						if($starttime == "") {
						$endisoDate = date('Y-m-d', strtotime("$enddate"));
						} else {
						$endisoDate = date('Y-m-d\TH:i:s', strtotime("$enddate $endtime"));
						}

						?>
						title: '<?php echo $event->title() ?>',
						url: '<?php echo $event->url() ?>',
						start: '<?php echo $startisoDate ?>',
						end: '<?php echo $endisoDate ?>',
					},
				<?php endforeach ?>
				]
			});

		});

	</script>