This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
As you can see my date.of.birth format is Y-m-d, so I hope to change my D.O.B (tarikh lahir) format to d-m-Y,please help.
This is model : Search_model.php
<?php
class Search_model extends CI_Model {
public function get_results($search_term='default')
{
// Use the Active Record class for safer queries.
// $this->load->helper('share_function');
$this->db->select('pesakit.rn,pesakit.nama,pesakit.tarikhlahir,jantina.nama as jantina,agama.nama as agama,bangsa.nama as bangsa');
$this->db->from('lifeline.pesakit as pesakit');
$this->db->join('jantina','jantina.kod = pesakit.jantina');
$this->db->join('agama','agama.kod = pesakit.agama');
$this->db->join('bangsa','bangsa.kod = pesakit.bangsa');
$this->db->where('rn',alphaToNumber($search_term));
// Execute the query.
$query = $this->db->get();
// Return the results.
return $query->result_array();
}
}
This is view : search_results.php
<div>
<?php
foreach ($results as $val)
{
echo 'RN : '; echo numberToAlpha( $val['rn']); echo "<br/>";
echo 'Name : '; echo $val['nama']; echo "<br/>";
echo 'Date.of.Birth : '; echo $val['tarikhlahir']; echo "<br/>";
echo 'Age:' ; echo calculateCurrentAge ($val['tarikhlahir']); echo "<br/>";
echo 'Gender : '; echo $val['jantina']; echo "<br/>";
echo 'Race : '; echo $val['bangsa']; echo "<br/>";
echo 'Religion : '; echo $val['agama'];
}
?>
</div>
Check this code. Hopefully, it will help you.
$dob = "2012-03-08";
echo date("d-m-Y", strtotime($dob));
//output date
08-03-2012
You need to create a function dateFormat in Helper.
function dateFormat($date, $format = 'd-M-Y'){
return date($format, strtotime($date));
}
We can call this :
<td><?php echo dateFormat($val['tarikhlahir']);?></td>
Using above function, date will be returned as its in default parameter.
While it can be changed where we will call this function.
For example :
<td><?php echo dateFormat($val['tarikhlahir'], 'm-d-Y');?></td>
Try not to use the semicolon to concatenate the strings.
Use "Dot" to do this.
echo 'Date.of.Birth : ' . date('d m Y', strtotime($val['tarikhlahir']) . "<br/>";
date('d-m-Y', strtotime($the_date_you_want_to_convert));
http://php.net/manual/en/function.strtotime.php
As per the #Alex answer.
As per my knowledge, there is no separate date function in CodeIgniter.
You can use PHP date function.
http://php.net/manual/en/function.date.php
http://php.net/manual/en/function.strtotime.php
$val['tarikhlahir']='1959-01-30';
$date=date('d-m-Y',strtotime($val['tarikhlahir']));
echo $date;
Please replace your the code as given below
<div>
<?php
foreach ($results as $val)
{
echo 'RN : '; echo numberToAlpha( $val['rn']); echo "<br/>";
echo 'Name : '; echo $val['nama']; echo "<br/>";
$originalDate = $val['tarikhlahir'];
$newDate = date("d-m-Y", strtotime($originalDate));
echo 'Date.of.Birth : '; echo $newDate; echo "<br/>";
echo 'Age:' ; echo calculateCurrentAge ($val['tarikhlahir']); echo "<br/>";
echo 'Gender : '; echo $val['jantina']; echo "<br/>";
echo 'Race : '; echo $val['bangsa']; echo "<br/>";
echo 'Religion : '; echo $val['agama'];
}
?>
</div>
This may help you..Thanks!
since nobody mentioned it - imho the safest way to do this is the DateTime::createFromFormat function.
Something like that should work
<div>
<?php
foreach ($results as $val)
{
$objDateTime = DateTime::createFromFormat('Y-m-d', $val['tarikhlahir']);
echo 'RN : '; echo numberToAlpha( $val['rn']); echo "<br/>";
echo 'Name : '; echo $val['nama']; echo "<br/>";
echo 'Date.of.Birth : '; echo $objDateTime->format('d-m-Y'); echo "<br/>";
echo 'Age:' ; echo calculateCurrentAge ($val['tarikhlahir']); echo "<br/>";
echo 'Gender : '; echo $val['jantina']; echo "<br/>";
echo 'Race : '; echo $val['bangsa']; echo "<br/>";
echo 'Religion : '; echo $val['agama'];
}
?>
</div>
Related
This code with a couple of functions to modify a date works:
<?php
function date_up($input_date){
echo date("Y-m-d",strtotime('+1 day',strtotime($input_date)));
}
function date_up_friday($input_date){
echo date("Y-m-d",strtotime('+3 day',strtotime($input_date)));
}
function date_down($input_date){
return date("Y-m-d",strtotime('-1 day',strtotime($input_date)));
}
function date_down_monday($input_date){
echo date("Y-m-d",strtotime('-3 day',strtotime($input_date)));
}
echo 'Input date: ';echo $date='2017-09-25';
echo '<BR><BR>';
echo 'Down 1: ';
echo date_down($date);
echo '<BR>';
echo 'Down 3: ';date_down_monday($date);
echo '<BR>';
echo 'Up 1: ';date_up($date);
echo '<BR>';
echo 'Up 3: ';date_up_friday($date);
?>
I am trying to send the output from the function to another page when the hyperlink. New code shown below. But no date is sent in the hyperlink. I don't understand this and I apologise if their is a simple explanation. I am new to working with PHP.
Many thanks in advance !
echo 'Input date: ';echo $date='2017-09-25';
echo '<BR><BR>';
echo '<a href="Dev/Update_date_shortfall.php?date="';
echo date_down($date);
echo '">Down 1: </a>';
echo date_down($date);
echo '<BR>';
echo 'Down 3: ';date_down_monday($date);
echo '<BR>';
echo 'Up 1: ';date_up($date);
echo '<BR>';
echo 'Up 3: ';date_up_friday($date);
You need to send it like this:
echo 'Down 1: ';
And in Update_date_shortfall.php, you need to
echo $_GET['date'];
you can try this
echo 'Down 1: ';
working demo : https://ideone.com/bZNwMr
I need a regex expert who can split this single string variable into multiple variables. Coming data from source having line break but not showing \n.
I will be very thankful.
$text_utf8_test = "lat:24.910717 lon:67.073710
speed:0.00
T:17/02/22 17:00
http://maps.google.com/maps?f=q&q=24.910717,67.073710&z=16
Pwr: ON Door: OFF ACC: OFF";
Expected PHP variables after spliting
echo $lat;
echo $long;
echo $speed;
echo $time;
echo $pwr;
echo $door;
echo $acc;
Expected Output Result
24.910717 /*This value can be vary due to dynamic update from location*/
67.073710
0.00 /*this can be from zero to 320*/
ON /*this can be ON or OFF*/
OFF /*this can be ON or OFF*/
OFF /*this can be ON or OFF*/
$text_utf8_test = "lat:24.910717 lon:67.073710
speed:0.00
T:17/02/22 17:59
http://maps.google.com/maps?f=q&q=24.910717,67.073710&z=16
Pwr: ON Door: OFF ACC: OFF";
$phone_test = "88888888888";
echo $text_utf8_test;
preg_match('/([0-9.-]+).+?([0-9.-]+)/', $text_utf8_test, $matches); /*For lat long exclusively*/
//preg_match('/OFF/', $text_utf8_test, $test);
$keywords = preg_split("/[\s,]+/", $text_utf8_test); /*For spliting this whole string*/
preg_match('~speed:([-+]?[0-9]*\.?[0-9]*)~', $keywords[2], $speed ); /*For grabing speed*/
preg_match('/(\d{2})\/(\d{2})\/(\d{2})(\d{2}):(\d{2})$/', $keywords[3].$keywords[4], $split_date_time ); /*For grabing date time*/
/*Spliting and combining date time into sql format*/
if(!empty($split_date_time[3])){
$date = $split_date_time[3].$split_date_time[2].$split_date_time[1];
echo "</br>";
$date_sanitized1 = strtotime($split_date_time[2]."/".$split_date_time[3]."/".$split_date_time[1]);
$date_sanitized2 = date('Y-m-d',$date_sanitized1);
$date_time = $date_sanitized2." ".$split_date_time[4].":".$split_date_time[5].":"."00.000" ;
}
if((!empty($keywords[2])) && (!empty($keywords[8])) && (!empty($keywords[10])) && (!empty($keywords[12]))){
$speed=$speed[1];
$power=$keywords[8];
$door=$keywords[10];
$acc=$keywords[12];
}
$lat=$matches[1];
$long=$matches[2];
if ((!empty($lat)) && (!empty($long)) && (!empty($speed)) && (!empty($date_time)) && (!empty($power)) && (!empty($door)) && (!empty($acc)) ){
echo "</br>";
echo $lat;
echo "</br>";
echo $long;
echo "</br>";
echo $speed;
echo "</br>";
echo $date_time;
echo "</br>";
echo $power;
echo "</br>";
echo $door;
echo "</br>";
echo $acc;
echo "</br>";
}else{
echo "noting";
}
Output
24.910717
67.073710
0.00
2017-02-22 17:59:00.000
ON
OFF
OFF
Hy,
I just cant figure it out how to get this to work.
How do I post the home>name, home>score to the database (the same for away) and send the status, rounds and datetime to the database.
But if there is a change in the json url than update the database and if there is a new match insert it.
this all needs to be done automatically.
[
{
"url":"http://www.gosugamers.net/counterstrike/tournaments/10423-starladder-cis-minor-championship-cologne-2016/2865-playoffs/10426-playoffs/matches/113497-worst-players-vs-team-empire",
"home":{
"name":"Worst Players",
"url":"/counterstrike/teams/15191-worst-players",
"country":"Russian Federation",
"rank":57,
"score":1
},
"away":{
"name":"Team Empire.",
"url":"/counterstrike/teams/14464-team-empire",
"country":"Russian Federation",
"rank":40,
"score":2
},
"status":"Complete",
"type":"counterstrike",
"rounds":"Best of 3",
"valueBet":true,
"datetime":1462024800
},
{
"url":"http://www.gosugamers.net/counterstrike/tournaments/10296-esports-championship-series-season-1/2810-online-phase/10298-north-american-league/matches/111367-team-liquid-cs-vs-counter-logic-gaming-cs",
"home":{
"name":"Team Liquid.CS",
"url":"/counterstrike/teams/10561-team-liquid-cs",
"country":"United States",
"rank":10
},
"away":{
"name":"Counter Logic Gaming.CS",
"url":"/counterstrike/teams/10569-counter-logic-gaming-cs",
"country":"United States",
"rank":39
},
"status":"Upcoming",
"type":"counterstrike",
"rounds":"Best of 3",
"valueBet":true,
"datetime":1462068000
}
]
I have tried this code
<?php
include("../../check.php");
$urljson = "http://localhost:5000/matches";
$json = file_get_contents($urljson);
$data = json_decode($json, TRUE);
$home = $data[0]['home'];
$away = $data[0]['away'];
$status = $data[0]['status'];
$type = $data[0]['rounds'];
$timestamp = $data[0]['datetime'];
foreach($data as $array) {
echo $home['name'];
echo '<br>';
echo $home['score'];
echo '<br>';
echo $away['name'];
echo '<br>';
echo $away['score'];
echo '<br>';
echo $status;
echo '<br>';
echo $type;
echo '<br>';
echo $timestamp;
echo '<br>';
echo '<br>';
}
?>
but this is what the webpage gave me (it showed mutiple times the same result)
ANOX
Notice: Undefined index: score in C:\xampp\htdocs\main\dashboard\backend\info.php on line 18
Worst Players
Notice: Undefined index: score in C:\xampp\htdocs\main\dashboard\backend\info.php on line 22
Upcoming
Best of 3
1462100400
I hope someone can help me with this beceause I am stuck for like 4 days now.
You get the same data all the time cause you are using the wrong variables inside the loop. Use the $array variable in your case:
foreach($data as $array) {
echo $array['name'];
echo '<br>';
echo $array['score'];
echo '<br>';
... etc.
}
Maybe it would be good to call the variable $row or $match instead of $array for better readability.
You get the "Notice: Undefined..." because there are some JSON nodes where there is no score. Check for score before using it with
if (isset($array['score'])) {
// use it inside if:
echo $array['score']
}
Here the corrected code:
<?php
include("../../check.php");
$urljson = "http://localhost:5000/matches";
$json = file_get_contents($urljson);
$data = json_decode($json, TRUE);
foreach($data as $match) {
echo $match['home']['name'];
echo '<br>';
if (isset($match['home']['score'])) {
echo $match['home']['score'];
echo '<br>';
}
echo $match['away']['name'];
echo '<br>';
if (isset($match['away']['score'])) {
echo $match['away']['score'];
echo '<br>';
}
echo $match['status'];
echo '<br>';
echo $match['rounds'];
echo '<br>';
echo $match['datetime'];
echo '<br>';
echo '<br>';
}
?>
I have successfully put together a PHP script to read content of an xml file and output the result to HTML page. The only bit I am struggling with is how to format the output into a table.
PHP Script:
<?php
// Loading the XML file
$xml = simplexml_load_file("ftpxml.xml");
echo "<h2>".$xml->getName()."</h2><br />";
foreach($xml->children() as $ftpxml)
{
echo "PID : ".$ftpxml->attributes()->pid."<br />";
echo "Account : ".$ftpxml->attributes()->account." <br />";
echo "Time : ".$ftpxml->attributes()->time." <br />";
echo "<hr/>";
}
?>
HTML Result:
PID : 279
Account : account001
Time : 137
----------------------------------------------------------------
PID : 268
Account : account002
Time : 301
----------------------------------------------------------------
PID : 251
Account : account003
Time : 5
----------------------------------------------------------------
I am lost as to how to display each table headings and the corresponding contents. I am new to PHP so please guide me or if already answered else where, please provide link so I can learn from it.
Thanks
<?php
// Loading the XML file
$xml = simplexml_load_file("ftpxml.xml");
echo "<h2>".$xml->getName()."</h2><br />";
echo "<table>";
foreach($xml->children() as $ftpxml)
{
echo "<tr><td>PID : ".$ftpxml->attributes()->pid."</td></tr>";
echo "<tr><td>Account : ".$ftpxml->attributes()->account." </td></tr>";
echo "<tr><td>Time : ".$ftpxml->attributes()->time." </td></tr>";
}
echo "</table>";
?>
echo '<table>';
echo '<thead><tr><th>PID</th><th>Account</th><th>Time</th></tr></thead><tbody>';
foreach($xml->children() as $ftpxml)
{
echo '<tr>';
echo "<td>PID : ".$ftpxml->attributes()->pid."</td>";
echo "<td>Account : ".$ftpxml->attributes()->account."</td>";
echo "<td>Time : ".$ftpxml->attributes()->time." </td>";
echo '</tr>';
}
echo '</tbody></table>';
I assume you want to make the different values different fields in the table:
// Loading the XML file
$xml = simplexml_load_file("ftpxml.xml");
echo "<h2>".$xml->getName()."</h2><br />";
echo "<table><thead><tr><th>PID</th><th>Account</th><th>Time</th></tr></thead><tbody>";
foreach($xml->children() as $ftpxml)
{
echo '<tr>';
echo '<td>' . $ftpxml->attributes()->pid . "</td>";
echo '<td>' . $ftpxml->attributes()->account . "</td>";
echo '<td>' . $ftpxml->attributes()->time . "</td>";
echo '</tr>';
}
echo '</tbody></table>';
I have a mysql query that brings in a list of results, say 10, each with its own 'last edited' date, and was wondering if it was possible to display/echo/print ONLY the most recent of these dates using php?
Now I know I could do this with a mysql query, but I need to display all of the available results, but only 1 date, (the most recent).
Hope this makes sense. Any help greatly appreciated. S.
<?php
$availQuery=mysql_query("select * from isavailability");
$availResult=mysql_fetch_array($availQuery);
$date = $availResult['editDate'];
$newDate = date('D dS F Y',strtotime($date));
$time = date('G:i:s',strtotime($date));
echo '<p>This page was last updated on '.$newDate.' at '.$time.'</p>' . PHP_EOL;
while($availR=mysql_fetch_array($availQuery)) {
echo '<tr class="rows">'. PHP_EOL;
echo '<td><p>'.$availR['title'].'</p></td>'. PHP_EOL;
echo '<td>'; if ($availR['availability']==1) { echo $tick; } else { echo $cross; } echo '</td>'. PHP_EOL;
echo '<td>'; if ($availR['availability']==2) { echo $tick; } else { echo $cross; } echo '</td>'. PHP_EOL;
echo '<td>'; if ($availR['availability']==3) { echo $tick; } else { echo $cross; } echo '</td>'. PHP_EOL;
echo '</tr>'. PHP_EOL;
}
?>
It can be done in a single query: SELECT isavailabilit.*, MAX(editDate) AS LastEditDate FROM isavailabilit;
Use it this way in the loop: $availR['LastEditDate']
If your query is ordered by your timestamp then you could just grab the first one - if I understand your question:
$myDate = $myData[0]['myDateColumn'];
foreach($myData as $row)
{
echo $myDate . ' ' . $row['someotherColumn'] . "\n";
}
or something similar? Presuming your results are in an array.