thmsnhl Posted September 14, 2014 Posted September 14, 2014 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>
LostKobrakai Posted September 14, 2014 Posted September 14, 2014 You can get the timestamp of $page->week_begin with $page->getUnformatted("week_begin"). With the timestamp you can easily use all the php date functions, which includes stuff to add a few days: http://de1.php.net/manual/en/ref.datetime.php. 2
Philipp Posted September 14, 2014 Posted September 14, 2014 (edited) (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 September 14, 2014 by Philipp 1
LostKobrakai Posted September 14, 2014 Posted September 14, 2014 @Philipp It's only a timestamp if you choose no output-formatting in the datefield settings, otherwise it's a formatted string. That's why I always use getUnformatted(). It always works and I can use the standart formating set in the settings. 1
mr-fan Posted September 14, 2014 Posted September 14, 2014 (edited) $wdate = $page->week_begin //and add some days $wdateplus = date('Y-m-d',strtotime($wdate. ' + 6 days')); i was...to slow.... Edited September 14, 2014 by mr-fan
LostKobrakai Posted September 14, 2014 Posted September 14, 2014 //and add some days $wdateplus = date('Y-m-d',strtotime($wdate. ' + 6 days')); ...to slow.... Just out of curiosity, how does strtotime() devide between "d/m/Y" and "m/d/Y"? Both are posibilities for a pw date field and I don't really trust php here.
Martijn Geerts Posted September 14, 2014 Posted September 14, 2014 "d/m/Y" shouldn't be used, it's American notation. For european you should use a dot or a dash to separate.
LostKobrakai Posted September 14, 2014 Posted September 14, 2014 Ok, so I'll stick to getUnformatted(). It's saver to get the right timestamp, as long as both formats can be chosen in a datefield.
Martijn Geerts Posted September 14, 2014 Posted September 14, 2014 Ok, so I'll stick to getUnformatted(). It's saver to get the right timestamp, as long as both formats can be choosen in a datefield. , actually I never put OutputFormating on a date time field.
LostKobrakai Posted September 14, 2014 Posted September 14, 2014 I use it as standart format and getUnformatted() if I need to get another format.
mr-fan Posted September 14, 2014 Posted September 14, 2014 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
thmsnhl Posted September 15, 2014 Author Posted September 15, 2014 Wow! That's awesome, thank you guys for the help, I'll try it straight away.
thmsnhl Posted September 15, 2014 Author Posted September 15, 2014 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!
LostKobrakai Posted September 15, 2014 Posted September 15, 2014 You can save the first lines by using this. $timestamp = $page->getUnformatted("week_begin"); 2
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now