Welcome to the Cumulus Support forum.

Latest Cumulus MX V3 release 3.28.6 (build 3283) - 21 March 2024

Cumulus MX V4 beta test release 4.0.0 (build 4019) - 03 April 2024

Legacy Cumulus 1 release 1.9.4 (build 1099) - 28 November 2014
(a patch is available for 1.9.4 build 1099 that extends the date range of drop-down menus to 2030)

Download the Software (Cumulus MX / Cumulus 1 and other related items) from the Wiki

Having trouble formatting date!

Discussion of Ken True's web site templates

Moderator: saratogaWX

Post Reply
User avatar
William Grimsley
Posts: 833
Joined: Thu 22 Sep 2011 5:22 pm
Weather Station: Davis Vantage Vue
Operating System: Windows 7 Home Premium 64-bit
Location: Latitude: 50.70189285 Longitude: -3.30849957
Contact:

Having trouble formatting date!

Post by William Grimsley »

Hi Ken,

On my page: http://www.newton-poppleford-weather.co ... ecords.php

I've currently got this error.

Warning: printf() [function.printf]: Too few arguments in /home/newtonpo/public_html/wxrecords.php on line 74

I'm not sure what that means. I'm trying to do the same as I did before with the month gust on the ajax-dashboard.php, but it just won't work!

Here is my code:

Code: Select all

<?php printf('%02d/%02d',recordsbegandate); ?>
It looks ok, to me! But, not at all.

Or, has it got to do with this code?

Code: Select all

<?php
function fix_CU_record_date( $inDate) {
global $SITE;
  $EnglishMonths = array(
   'January','February','March','April','May','June',
   'July','August','September','October','November','December');

// 'at 2:45 PM on 16 October 2009'	
	if(preg_match('|at (.*) on (.*)$|',$inDate,$matches)) {
	  echo "<!-- inDate='$inDate' date='".$matches[2]."' time='".$matches[1]."' -->\n";
	  $rdate = $matches[2];
	  if (isset($SITE['monthNames'])) {
		// convert TO English for strtotime()
		foreach ($EnglishMonths as $i => $monthEN) {
		   $rdate = preg_replace('|'.$SITE['monthNames'][$i].'|i',$monthEN,$rdate);
		}
	  }  
	  $t = strtotime($rdate . ' ' .$matches[1]);
	  $outTime = date($SITE['dateOnlyFormat'] . ' ' .$SITE['timeOnlyFormat'],$t);
	  if (isset($SITE['langMonths'])) {
        // convert From English for return (will only work if long-format month names in $timeFormat)  
    	foreach ($EnglishMonths as $i => $monthEN) {
	       $outTime = preg_replace('|'.$monthEN.'|i',$SITE['langMonths'][$i],$outTime);
    	}  
	  }
	  echo "<!-- outTime='$outTime' -->\n";
	  return($outTime);
	}
	if(preg_match('|on (.*)$|',$inDate,$matches)) {
		echo "<!-- inDate='$inDate' date='".$matches[1]."' -->\n";
		$rdate = $matches[1];
		if (isset($SITE['monthNames'])) {
		  // convert TO English for strtotime()
		  foreach ($EnglishMonths as $i => $monthEN) {
			 $rdate = preg_replace('|'.$SITE['monthNames'][$i].'|i',$monthEN,$rdate);
		  }
		}  
		$t = strtotime($rdate);
		$outTime = date($SITE['dateOnlyFormat'],$t);
		if (isset($SITE['langMonths'])) {
		  // convert From English for return (will only work if long-format month names in $timeFormat)  
		  foreach ($EnglishMonths as $i => $monthEN) {
			 $outTime = preg_replace('|'.$monthEN.'|i',$SITE['langMonths'][$i],$outTime);
		  }  
		}
		echo "<!-- outTime='$outTime' -->\n";
		return($outTime);
	}
	if(preg_match('|(\S+) (\d+)$|',$inDate,$matches)) {
		echo "<!-- inDate='$inDate' month='".$matches[1]."' year='".$matches[2]."' -->\n";
		$rdate = $matches[1];
		if (isset($SITE['monthNames'])) {
		  // convert TO English for strtotime()
		  foreach ($EnglishMonths as $i => $monthEN) {
			 $rdate = preg_replace('|'.$SITE['monthNames'][$i].'|i',$monthEN,$rdate);
		  }
		}  
		
		$t = strtotime($rdate . ' ' . $matches[2]);
		$idx = date('n',$t); // month number 1=january etc...
		
		$outTime = $SITE['langMonths'][$idx-1] . ' ' . $matches[2];
		echo "<!-- outTime='$outTime' -->\n";
		return($outTime);
	}
	
	
	return(trim($inDate));

}
Please can you help?

Thanks

William
User avatar
saratogaWX
Posts: 1185
Joined: Wed 06 May 2009 5:02 am
Weather Station: Davis Vantage Pro Plus
Operating System: Windows 10 Professional
Location: Saratoga, CA, USA
Contact:

Re: Having trouble formatting date!

Post by saratogaWX »

To debug these issues, you have to start with the source (and format) of the data.

In CU-defs.php, there is

Code: Select all

$recordsbegandate = $WX['recordsbegandate'];
Looking at CUtags.php?sce=dump shows

Code: Select all

$WX['recordsbegandate'] = '25 September 2011';
Your code of

Code: Select all

<?php printf('%02d/%02d',recordsbegandate); ?>
is not proper PHP (missing the $ before recordsbegandate), but even so, the printf() function would NOT do what you want as the string feeding it is not in numerical format.

First you have to convert the string to a unix timestamp by

strtotime($recordsbegandate)

Then use the date() function to convert that into the format you want so something like

Code: Select all

<?php print date($SITE['dateOnlyFormat'],strtotime($recordsbegandate)); ?>
might do what you want.

It's all about knowing Where the data comes from, What is it's format, and Which functions can help you transform it to a different presentation.
User avatar
William Grimsley
Posts: 833
Joined: Thu 22 Sep 2011 5:22 pm
Weather Station: Davis Vantage Vue
Operating System: Windows 7 Home Premium 64-bit
Location: Latitude: 50.70189285 Longitude: -3.30849957
Contact:

Re: Having trouble formatting date!

Post by William Grimsley »

Hi Ken,

Hehe! You did it again! Thanks so much! Yes, I was looking in the CU-defs.php file, but, I can't get quite there yet. Though, I am learning and I'm not asking you for help as much as I was! :D

Thanks

William
User avatar
William Grimsley
Posts: 833
Joined: Thu 22 Sep 2011 5:22 pm
Weather Station: Davis Vantage Vue
Operating System: Windows 7 Home Premium 64-bit
Location: Latitude: 50.70189285 Longitude: -3.30849957
Contact:

Re: Having trouble formatting date!

Post by William Grimsley »

Hi Ken,

Sorry to bother you, again.

I'm also now having trouble with d/m format date. What I did is put your code and changed it:

Code: Select all

<?php print date($SITE['dateOnlyFormat'],strtotime($trfallmH)); ?>
But, it came up with that silly 01/01/1970 thing.

Why did that happen?

Please can you help, again? :oops:

Thanks

William
User avatar
saratogaWX
Posts: 1185
Joined: Wed 06 May 2009 5:02 am
Weather Station: Davis Vantage Pro Plus
Operating System: Windows 10 Professional
Location: Saratoga, CA, USA
Contact:

Re: Having trouble formatting date!

Post by saratogaWX »

The strtotime() function in PHP expects dates to be in certain formats: (see docs at PHP.net

m/d/yyyy
dd-Mon-yyyy
d Month yyyy

You can't feed it a d/m/yyyy formatted date.. it will return a timestamp of 0 (zero) which in unix time is 1/1/1970 00:00:00

With this info, you can figure out what to do.
User avatar
William Grimsley
Posts: 833
Joined: Thu 22 Sep 2011 5:22 pm
Weather Station: Davis Vantage Vue
Operating System: Windows 7 Home Premium 64-bit
Location: Latitude: 50.70189285 Longitude: -3.30849957
Contact:

Re: Having trouble formatting date!

Post by William Grimsley »

Hi Ken,

Sorry, I don't get it. I don't know what to do, sorry. :oops:

Thanks

William
User avatar
saratogaWX
Posts: 1185
Joined: Wed 06 May 2009 5:02 am
Weather Station: Davis Vantage Pro Plus
Operating System: Windows 10 Professional
Location: Saratoga, CA, USA
Contact:

Re: Having trouble formatting date!

Post by saratogaWX »

The template set doesn't provide a variable named '$trfallmH', so it is a default 0 (with a Notice: type error saying uninitialized variable).

I think you wanted to use $WX['TrfallhH'] as the variable instead. Looking at CUtags.php?sce=dump on your site it shows
a value of:

$WX['TrfallhH'] = 'at 20:54 on 02 June 2012';

The contents of that variable are NOT directly compatible with strtotime() PHP function so you can either print it as-is, or
write some code to remove the 'at' and 'on' words and swap-around the time and date strings so it looks like

'02 June 2012 20:54'

then strtotime() could handle it.
User avatar
mcrossley
Posts: 12756
Joined: Thu 07 Jan 2010 9:44 pm
Weather Station: Davis VP2/WLL
Operating System: Bullseye Lite rPi
Location: Wilmslow, Cheshire, UK
Contact:

Re: Having trouble formatting date!

Post by mcrossley »

or alter the Cumulus tag that populates that variable to use a date/time format that you want. Of course that will also affect any other place that the variable is used as well.
BCJKiwi
Posts: 1255
Joined: Mon 09 Jul 2012 8:40 pm
Weather Station: Davis VP2 Cabled
Operating System: Windows 10 Pro
Location: Auckland, New Zealand
Contact:

Re: Having trouble formatting date!

Post by BCJKiwi »

In a couple of places I have created another tag with a slightly different name to use the new format thus ensuring the original tag remains as expected if used somewhere else.
e.g.
LastRainTipISO|2013-02-05 07:29:|: which is not the same a the other date tags in NZ
so I created additional tags to provide
LastRainTip|at 07:29 on 05/02/2013:|:
LastRainTipTime|at 07:29:|:
to get the same format as all the other default date formats plus a time only tag.
User avatar
William Grimsley
Posts: 833
Joined: Thu 22 Sep 2011 5:22 pm
Weather Station: Davis Vantage Vue
Operating System: Windows 7 Home Premium 64-bit
Location: Latitude: 50.70189285 Longitude: -3.30849957
Contact:

Re: Having trouble formatting date!

Post by William Grimsley »

saratogaWX wrote:The template set doesn't provide a variable named '$trfallmH', so it is a default 0 (with a Notice: type error saying uninitialized variable).

I think you wanted to use $WX['TrfallhH'] as the variable instead. Looking at CUtags.php?sce=dump on your site it shows
a value of:

$WX['TrfallhH'] = 'at 20:54 on 02 June 2012';

The contents of that variable are NOT directly compatible with strtotime() PHP function so you can either print it as-is, or
write some code to remove the 'at' and 'on' words and swap-around the time and date strings so it looks like

'02 June 2012 20:54'

then strtotime() could handle it.
No, there is a tag called '$trfallmH' and the tag you gave me is hourly rainfall! :bash:
User avatar
William Grimsley
Posts: 833
Joined: Thu 22 Sep 2011 5:22 pm
Weather Station: Davis Vantage Vue
Operating System: Windows 7 Home Premium 64-bit
Location: Latitude: 50.70189285 Longitude: -3.30849957
Contact:

Re: Having trouble formatting date!

Post by William Grimsley »

Hi guys,

Sorry, but I've given up, as I don't know what all your posts mean.

So, I just put this code into my http://www.newton-poppleford-weather.co ... ecords.php page.

Code: Select all

<div id="main-copy">

  
	<h1><?php langtrans('All Time Records'); ?></h1>
    
<?php if(file_exists("record.htm")) {
	 include_once("record.htm");
} else { echo "<p>Sorry.. record.htm file is not available.</p>\n"; }
?>

<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
</div><!-- end main-copy -->
The dates are now all in dd/mm/yyyy and the page looks exactly the same.

I also, did the same thing for my yearly and monthly record pages, which can be seen here:

Yearly: http://www.newton-poppleford-weather.co ... ordsyr.php

Monthly: http://www.newton-poppleford-weather.co ... ordsmo.php

Thanks for your help,

William
Last edited by William Grimsley on Tue 12 Feb 2013 7:00 pm, edited 3 times in total.
User avatar
saratogaWX
Posts: 1185
Joined: Wed 06 May 2009 5:02 am
Weather Station: Davis Vantage Pro Plus
Operating System: Windows 10 Professional
Location: Saratoga, CA, USA
Contact:

Re: Having trouble formatting date!

Post by saratogaWX »

There is no variable $trfallmH defined in the CU-defs.php, unless you added it there. :bash:

You're responsible for debugging your own additions to the code. You asked for assistance, and it was given. Conversation is over.
User avatar
mcrossley
Posts: 12756
Joined: Thu 07 Jan 2010 9:44 pm
Weather Station: Davis VP2/WLL
Operating System: Bullseye Lite rPi
Location: Wilmslow, Cheshire, UK
Contact:

Re: Having trouble formatting date!

Post by mcrossley »

<opinion_mode>...

William, you seem to makes lots of changes to things you don't understand, then expect everyone on the forum to help you out. People provide solutions and you plug them in seemingly without any comprehension of why.

You need to take the time to understand firstly what you are changing, and secondly when people provide solutions, understand what has been done to make the change and why. By taking the time in learning things (some basic PHP?) you will be able to help yourself and stop asking very similar questions all the time. (Google is a great resource too ;) )

Even though I am not directly involved you seem to be employing the forum to re-write code virtually line by line. People will quickly get tired of this approach.

</opinion_mode>

That's all I say on the subject. Out of here...
User avatar
William Grimsley
Posts: 833
Joined: Thu 22 Sep 2011 5:22 pm
Weather Station: Davis Vantage Vue
Operating System: Windows 7 Home Premium 64-bit
Location: Latitude: 50.70189285 Longitude: -3.30849957
Contact:

Re: Having trouble formatting date!

Post by William Grimsley »

Hi Mark,

Yes, I am like that! I am starting to use http://www.php.net slowly, so that I can learn before asking! LOL :D

Though, I am not trying to make people re-write code, line by line... :oops:

Thanks for your help,

William
Post Reply