Jump to content

Manipulate output date from datetime-field


thmsnhl
 Share

Recommended Posts

Hi! 

I'm trying to output the date of a datetime-field and right next to it the date + 6 days.

Could anybody help me to do this?

<tr>
   <td>Ausbildungswoche vom:</td>
   <td><?php echo $page->week_begin ?></td>
   <td>Ausbildungswoche bis:</td>
   <td><?php echo $page->week_end ?></td> //should be week_begin + 6 days
</tr>
Link to comment
Share on other sites

(Correct me if I'm wrong but) The datetime field outputs a UNIX timestamp per default, right? If not, you could convert it to one using the PHP DateTime function.  You can get the unformatted UNIX timestamp as LostKobrakai described above.

With a timestamp, you could simply add 6 days to the date and output it with the date() function:

<? 
//Future date is calculated on page. 86400 = seconds for a day)
$future_date = $page->getUnformatted("week_beginn") + (86400 * 6 );

//Output the timestamp, formated to something like 2014.08.20
echo date('Y.m.d',$future_date);
EDIT: Using getUnformatted() Edited by Philipp
  • Like 1
Link to comment
Share on other sites

yes indeed this is much safer....other snippets to splitt a datefield maybe usefull for panophobie...

//get the date
$mydate = $page->week_beginn;

//splitt a timestamp
$timestamp = strtotime($mydate );

$month = strftime("%b",$timestamp);
$day = strftime("%d",$timestamp);
$year = strftime("%Y",$timestamp);

this should work - so i've build a calender with outputting single parts of a date....adding days, minutes to a timestamp is the better way like LostKobrakai described.

regards mr-fan

Link to comment
Share on other sites

So, I have tried it with your suggestions, but did not get the expected result. 

Luckily one of our developers came and helped me to write this:

	$mydate = $page->week_begin;

	$mydateArray = explode('.', $mydate);

	foreach ( $mydateArray as $index => $nummer ) {
		$mydateArray[$index] = ltrim($nummer, '0');
	}

	$timestamp = mktime(0, 0, 0, $mydateArray[1], $mydateArray[0], $mydateArray[2]);

	$dateObj = new \DateTime(date('Y-m-d', $timestamp));
	$dateObj->modify('+6 days');

	echo $dateObj->format('d.m.Y');

Which works fine for me.

Thank you anyway!

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
 Share

  • Recently Browsing   0 members

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