Jump to content

separate OutPut of Date / Time from FieldType DateTime


Recommended Posts

Hi all

I almost bang my head against the desk, trying to separate date and time in the the output. I would love to use only one DateTime field for a little Event list. 

I used this code

$content .= "<p>Datum: {$event->zeit()}";
$content .= "<br />Zeit: {$event->zeit}";

I tried to out put the date only by $content .= "<p>Datum: {$event->zeit("d-m-Y")}";

but I get errors.

Is there a way to output time and date separate or do I have to use two fields?



Link to comment
Share on other sites

Hey Beat, no need to bang your head ;)

If you didn't change the output in the field settings, you should be getting a timestamp (1475743564). You can just break it down into date and time like this:

echo "<p>Datum: " . strftime( '%d/%m/%Y',  $event->zeit );
echo "<br />Zeit: " . strftime( '%H:%M:%S',  $event->zeit );

The strings that I'm passing to the function are time representations from this list: http://php.net/manual/en/function.strftime.php

You can do something more elaborate like this:

strftime( '%A, %e of %B of the year %Y',  $event->zeit );

which would output "Thursday, 6 of October of the year 2016"

In that case, you would probably want to output them in German, so you have to set the locale if the system isn't already:

setlocale(LC_ALL, "de_CH", "de_CH.utf-8", german")
strftime( '%A, %e of %B of the year %Y', $event->zeit );





  • Like 1
Link to comment
Share on other sites

May I ask, where you got this code? $event->zeit is neither a function, nor a object you could call methods on. It's either a timestamp (integer) or a formatted string.

I'm using this hook in my ready.php to have something a bit like you wanted to use:

$wire->addHook("Page::getDate", function(HookEvent $event){
	$page = $event->object;
	$fieldName = $event->arguments(0);
	$dateFormat = $event->arguments(1);
	$empty = $event->arguments(2) ?: '';

	$field = $page->fields->get($fieldName);

	if(($field instanceof Field && $field->type == "FieldtypeDatetime") || $fieldName == "created" || $fieldName == "modified"){
		$function = strpos($dateFormat, "%") !== false ? 'strftime' : 'date';
		$raw = $page->getUnformatted($fieldName);
		return $event->return = $raw ? $function($dateFormat, $raw) : $empty;

It allows you to use this on datetime fields, no matter of the field settings:

// date() syntax
// => 04.02.2016
$page->getDate('zeit', 'd.m.Y');

// strftime syntax
// => 4 Februar 2016
$page->getDate('zeit', '%e %B %Y');

// empty field value
// => Nicht terminiert.
$page->getDate('empty_zeit', '%e %B %Y', 'Nicht terminiert.');

// invalid field
// => null
$page->getDate('ziet', 'd.m.Y');


  • Like 3
Link to comment
Share on other sites

Hi Diogo , Lost Koprakai, 

thanks Diogo, your solutions works, as usual.

@Lost Koprakai

this is my original code.

$events = $page->events;

foreach($events as $event){

	$programm = $event->file ? "<br /><a href='{$event->file->url}' title='{$event->file->name}' target='_blank'>{$event->file->description} herunterladen</a>" : "";

	$content .= "<div class='event'>";
	$content .= "<h2>{$event->firstname}</h2>";
	$content .= "<p>Datum:"{$event->zeit()}";
	$content .= "<br />Zeit: {$event->zeit}";
	$content .= "<br />Ort: {$event->sirname}";
	$content .= "<br />{$event->summary}";
	$content .= $programm;
	$content .= "</p></div>";


I will look into your proposal as well. Thanks a lot.

Link to comment
Share on other sites

Just a small correction on your code. In this case it doesn't apply, because $event->zeit() is not an actual function, but in the case that it was, you could never call it inside the curly braces, because with that method of echoing strings the functions are not processed. In that case you would want to do:

$content .= "<p>Datum:" . $event->zeit();


Edited by diogo
wrong! see LostKobrakai post below
Link to comment
Share on other sites

@diogo That's not true, it won't work without curly braces, but it does with them.

class Greeter{
	public function greet() {
		return 'Hi!';

$greeter = new Greeter;

echo "Trigger greeter: {$greeter->greet()}";
// Trigger greeter: Hi! 


If you're not sure what you can do with a field, just var_dump($page->field) and it'll tell you what type the field does return. Only objects can give you the possibility to further customize the output via $page->field() or $page->field->someMethod(). A Datetime field will only return an integer or string.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...