This question already has answers here:
Convert a date format in PHP [duplicate]
(18 answers)
Closed 3 years ago.
XML is outputting time like this:
<time from="2020-02-03T12:00:00" to="2020-02-03T18:00:00" period="2"
^ this is dynamic and changes all the time ^
I would want to display it like this:
Feb 12:00 - 18:00
This is my PHP:
$url = ('https://www.yr.no/sted/Norge/Oslo/Oslo/Oslo/varsel.xml');
$feed = simplexml_load_file($url) or die('Can not connect to server');
$result = array();
foreach ($feed->forecast->tabular->time as $content) {
array_push($result, [ "from" => (string)$content['from'],
"to" => (string)$content['to'],
'symbol' => (string)$content->symbol['name'],
'temperature' => (string)$content->temperature['value'],
'windDirection' => (string)$content->windDirection['code'],
'windSpeed' => (string)$content->windSpeed['mps'],
]);
}
This is my html:
<section>
<div class="tbl-header">
<table cellpadding="0" cellspacing="0" border="0">
<thead>
<tr>
<th>Time</th>
<th>Weather</th>
<th>Temperature</th>
<th>Wind</th>
</tr>
</thead>
</table>
</div>
<div class="tbl-content">
<table cellpadding="0" cellspacing="0" border="0">
<tbody>
<?php foreach ($result as $value) { ?>
<tr>
<td>Bergen <br /><?php echo $value['from'] ?> til <?php echo $value['to'] ?></td>
<td><?php echo $value['symbol'] ?></td>
<td><?php echo $value['temperature'] ?> °C</td>
<td><?php echo $value['windSpeed'] ?> m/s fra <?php echo $value['windDirection'] ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</section>
how can I do that the best way by using php? Any help is greatly appreciated.
UPDATE : Simple solution With php self.
<td>Bergen <br /><?php echo date("j M, g:i", strtotime($value['from'])); ?> - <?php echo date("g:i", strtotime($value['to'])); ?></td>
Result : 3 Feb, 2:00 - 6:00
More examples : https://www.php.net/manual/en/function.date.php
With custom function
This function shortens months names
function shorten_text($text, $max){
$text = substr($text, 0, $max);
$text_son = strrchr($text, " ");
$text = str_replace($text_son,"", $text);
return $text;
}
You need to format date like this
<td>Bergen <br /><?php echo date('d', strtotime($value['from'])).".". shorten_text(date("F", strtotime($value['from'])), 3) ." ". date('H:i', strtotime($value['from']));?> til <?php echo date('d', strtotime($value['to'])).".". shorten_text(date("F", strtotime($value['to'])), 3) ." ". date('H:i', strtotime($value['to']));?></td>
This will give you result like 03.Feb 13:00 til 03.Feb 18:00. play with it and turn as you like.
Related
<table class="table row-border order-column">
<thead>
<tr>
<th></th>
<?php
$days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
for($i=1;$i<=$days;$i++)
{
?>
<th><?php echo $i; ?></th>
<?php
}
?>
</tr>
</thead>
<tbody>
<?php
foreach ($student as $key => $value)
{
?>
<tr>
<td class="cap"><?php echo $value->name; ?></td>
<?php
for($i=1;$i<=$days;$i++)
{
$result = $this->db->select('*')->from('attendance')->where('studentID',$value->studentID)->where('day(date)', date($i))->where('month(date)', date($month))->where('Year(date)', date($year))->get()->row();
if(empty($result))
{
$date = date('Y-m-d');
$current_date = date('d',strtotime($date));
if($i < $current_date)
{
?>
<td class="absent">A</td>
<?php
}
else
{
?>
<td></td>
<?php
}
}
else
{
$dateday = date('d',strtotime($result->date));
if($i == $dateday)
{
?>
<td class="present">P</td>
<?php
}
}
}
?>
</tr>
<?php
}
?>
</tbody>
In the above code I want to show absent while compare $current_date and $days but now what happen here only current month date show absent and previous month date show blank. Now, What am I going to do I don't want to show absent in future date. So, How can I do this? Please help me.
Thank You
Use Unix timestamp for your calculation and after that change Unix to gmt or utc time using ready functions to show.
This code shows every info from an XML. I want to only display row number one (first from/to, symbol temperature etc)
<?php
$url = ('https://www.yr.no/sted/Norge/oslo/oslo/oslo/varsel_time_for_time.xml');
$feed = simplexml_load_file($url) or die('Can not connect to server');
$result = array();
foreach ($feed->forecast->tabular->time as $content) {
array_push($result, [ "from" => (string)$content['from'],
"to" => (string)$content['to'],
'symbol' => (string)$content->symbol['name'],
'symbol-icon' => (string)$content->symbol['var'],
'temperature' => (string)$content->temperature['value'],
'windDirection' => (string)$content->windDirection['name'],
'windSpeed' => (string)$content->windSpeed['mps'],
'windType' => (string)$content->windSpeed['name'],
]);
}
?>
<button class="collapsible"><div class="tbl-content">
<table cellpadding="0" cellspacing="0" border="0">
<tbody>
<?php foreach ($result as $value) { ?>
<tr>
<td>Oslo</td>
<td><?php echo date("j. M", strtotime($value['from'])); ?> kl.<?php echo date("G", strtotime($value['from'])); ?></td>
<td><img src="http://yr.github.io/weather-symbols/png/100/<?php echo $value['symbol-icon'];?>.png" /></td>
<td><?php echo $value['temperature'] ?> °C</td>
<td><?php echo $value['windType'] ?>, <?php echo $value['windSpeed'] ?> m/s fra <?php echo $value['windDirection'] ?></td>
<td>Longtherm</td>
<td>Hour</td>
</tr>
<?php } ?>
</tbody>
</table>
</div></button>
quite unexperienced, any help is greatly appreciated
Just one of the rows? Remove the foreach! Just use $result[0] in place of $value. :-)
A defined number of rows? Use a for loop:
for ($x = 0; $x < $limit; $x++) {
$value = $result[$x];
// etc
}
I have a small invoice web tool.
So I would like to find out the total amount of all positions of items and if it reached a maximum level it should print the following positions on the second page of the invoice.
I use the sample code to "design" my invoices:
http://codepen.io/rafaelcastrocouto/pen/LFAes/
My idea is: first count all characters of the foreach loop.
But I don't know how I should continue?
Please give me some hints. Thanks
EDIT: here is my foreach loop code:
<?php foreach( $items as $it): ?>
<tr class="even">
<td style="width:8%;"><?php echo $pos; ?></td>
<td style="width:47%;"><?php
$string = $it->invoice_item_text;
if (strlen($string) > 350) $string = substr($string,0,350).'...';
echo $string;
?></td>
<td style="width:15%;">
<?php if ($it->invoice_item_type == 3) {$date = date('M Y', strtotime($it->invoice_item_date)); } else { $date = date('d.m.Y', strtotime($it->invoice_item_date)); } ?>
<?php echo $date; ?>
</td>
<td style="width:20%;"><?php if ($it->invoice_item_time != '0') { if ($it->invoice_item_type == 1 or $it->invoice_item_type == 2) echo gmdate('H:i', $it->invoice_item_time);}; ?></td>
<td style="width:10%;"><?php echo number_format($it->invoice_item_sum, 2, ',', ' '); ?> €</td>
</tr>
<?php $pos++; ?
<?php endforeach; ?>
Use php Array as session variable
$_SESSION['items'] = $items;
if you store value in session you can use this value any page
I am a new learner of PHP, now I want to use it to do a calendar like the following. There are something that I don't know.
How to get the first day of the month is sunday or another?
How to output all the days of the month and emphasize today?
The code:
<table cellspacing="0" cellpadding="3" bordercolor="#000000" border="1" style="width: 105px; border-collapse: collapse;">
<tbody>
<tr>
<td colspan="7"><?php echo date('F')." ".date('Y');?></td>
</tr>
<tr>
<td>Su</td>
<td>M</td>
<td>Tu</td>
<td>W</td>
<td>Th</td>
<td>F</td>
<td>Sa</td>
</tr>
<?php
$numrows = ceil(date('t')/7);
for($k=1;$k<=$numrows;$k++){
?>
<tr><td></td></tr>
<?php }?>
</tbody>
</table>
I don't know how to output the following days.
First Day of Month
date('d', strtotime('2012-04-01'));
Replace year and month with actual year and month you want to check. Leave the 01 for the first day.
http://php.net/manual/en/function.date.php
Output Days of Month and Emphasize Today
You can use date() with no second parameter to get the current time formatted however you want. To output the days of the month, you'd need to know how you wanted them formatted, so that's up to you to figure out. But, to get the number of days in a given month, you can use:
date('t')
That will return a number between 28 and 31, depending on how long the month is.
You really should read this: http://www.php.net/manual/en/ref.datetime.php
You never need these things nowadays when http://jqueryui.com/demos/datepicker/ is available, but if you want here is the code I wrote about 10 years ago, lol :)
<?
function my_calendar($fill=array()) {
if (isset($_GET['y'])) $y=$_GET['y'];
if (isset($_GET['m'])) $m=$_GET['m'];
if (isset($_GET['date']) AND strstr($_GET['date'],"-")) list($y,$m)=explode("-",$_GET['date']);
if (!isset($y) OR $y < 1970 OR $y > 2037) $y=date("Y");
if (!isset($m) OR $m < 1 OR $m > 12) $m=date("m");
$month_stamp=mktime(0,0,0,$m,1,$y);
$month_name=date("M",$month_stamp);
$day_count=date("t",$month_stamp);
$weekday=date("w",$month_stamp);
if ($weekday==0) $weekday=7;
$start=-($weekday-2);
$last=($day_count+$weekday-1) % 7;
if ($last==0) $end=$day_count; else $end=$day_count+7-$last;
$today=date("Y-m-d");
$prev=date('?\m=m&\y=Y',mktime (0,0,0,$m-1,1,$y));
$next=date('?\m=m&\y=Y',mktime (0,0,0,$m+1,1,$y));
$i=0;
?>
<table border=1 cellspacing=0 cellpadding=2>
<tr>
<td colspan=7>
<table width="100%" border=0 cellspacing=0 cellpadding=0>
<tr>
<td align="left"><<<</td>
<td align="center"><? echo $month_name," ",$y ?></td>
<td align="right">>>></td>
</tr>
</table>
</td>
</tr>
<tr><td>Mon</td><td>Tue</td><td>Wed</td><td>Th</td><td>Fri</td><td>Sat</td><td>Sun</td><tr>
<?
for($d=$start;$d<=$end;$d++) {
if (!($i++ % 7)) echo " <tr>\n";
echo ' <td align="center">';
if ($d < 1 OR $d > $day_count) {
echo " ";
} else {
$now="$y-$m-".sprintf("%02d",$d);
if (is_array($fill) AND in_array($now,$fill)) {
echo '<b>'.$d.'</b>';
} else {
echo $d;
}
}
echo "</td>\n";
if (!($i % 7)) echo " </tr>\n";
}
?>
</table>
<? } ?>
Use example
<?
if (isset($_GET['date'])) echo "Date picked: ".$_GET['date'];
my_calendar(array(date("Y-m-d")));
?>
it displays Monday first though. You need to remove some lines to get it to Sunday
I am having trouble with getting my year to echo properly.
Example: I have 2 news articles one with a year of 2011 and the other 2012. They are splitting into the correctly "year" div. But when it comes to rendering out the year, it works. But 2011 and 2012 are stacked above each other. Something like this.
2012
2011
5.1.12 My news article
Where 2011 year is suppose to go
5.1.11 My news article
Here is my code.
<?
$startYear = 2010;
$endYear = 2012;
for($y = $endYear; $y > $startYear -1; $y--) { ?>
<?
$news = query("SELECT * FROM news_entries
WHERE title > ''
AND published = 1
AND date >= '" . mktime(0,0,0,0,0,$y) . "'
AND date < '" . mktime(0,0,0,0,0,$y+1) . "'
ORDER BY date DESC");
?>
The rest of the query.
<? if($news['total']>0) { ?>
<h3><? echo $y; ?></h3>
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<th class="first" width="125">Date</th>
<th>News Headline</th>
<th width="140"> </th>
</tr>
<? do { ?>
<tr>
<td><span><? echo date("F d, Y",$news['data']['date']); ?></span></td>
<td><? echo $news['data']['headline']; ?></td>
<td>Learn More</td>
</tr>
<? } while($news['data'] = mysql_fetch_assoc($news['object'])); ?>
<? }} ?>
<tr>
<td colspan="2" class="last"></td>
<td class="last"> Back to Top</td>
</tr>
</table>
I agree with Kato's comment. Typically, you get all of the results and iterate over them, printing a new header when the data changes.
Here's how this type of thing is usually handled (pseudocode):
$sql = "SELECT * FROM news_enteries ORDER BY date DESC";
// Prepare statement
$stmt = $pdo->prepare($sql);
// Bind variables
...
// Execute query
$stmt->execute() or die();
$year_header = "";
while ($row = $stmt->fetch()) {
$year = date("Y", strtotime($row['date']));
// Do we need a new year header?
if ($year_header <> $year) {
$year_header = $year;
// Print year header
echo $year_header;
}
// Process rest of the row
...
}