I want to ask about how to convert timestamp from my database to ISO_8601 format in Codeigniter
Here is my controller:
public function index()
{
$data['viewme'] = $this->home_model->list_data();
$this->load->view('home', $data);
}
And this is my model:
function list_data(){
$sql = "SELECT * FROM post";
$query = $this->db->query();
$data = $query->result_array();
}
And this is my view:
<?php foreach($viewme as $data): ?>
<div class=" " title="<?php echo $data['time']?>"><?php echo $data['time']?></div>
<?php endforeach; ?>
The Problem: Time is still in timestamp format...
I have read codeigniter guide and found this
$format = 'DATE_ISO8601';
$waktu = time();
echo standard_date($format, $waktu);
But i dont know where I do to put that code into my codeigniter? any answer?
Many Thanks..
Core PHP code
$timestamp= '7861109272';
$iso8601 = date('c', $timestamp);
Manual
Related
Excuse me, i still learn about codeigniter's new.
i wanna ask what i have a simple query in view model like this below :
<h4 align="center"><?php echo '<b>'.strtoupper($data_mapel['nama_mapel']).' ('.$data_subjek_data['nama_subjek'].')</b>'; ?> </h4>
then, in controller model like this below :
public function view_soal($id_mapel,$id_paket){
// needed data
$data['data_subjek'] = $this->master->get_all_data('tb_subjek')->result();
$id_subjek = $this->input->post('subjek');
$where = array('id_mapel'=>$id_mapel,'id_subjek'=>$id_subjek);
$data['data_mapel'] = $this->master->find_data(array('id_mapel'=>$id_mapel),'tb_mapel')->row_array();
$data['data_subjek_data'] = $this->master->find_data(array('id'=>$id_subjek),'tb_subjek')->row_array();
//get data paket
$where_paket = array('id'=>$id_paket);
$data['data_paket'] = $this->master->find_data($where_paket,'tb_paket')->row_array();
//get data view
$data['data_view'] = $this->db->query("select a.*,b.nama_tema from tb_pertanyaan a inner join tb_tema b on a.id_tema = b.id_tema where a.id_tema IN(select id_tema from detil_paket where id_paket ='$id_paket')")->result();
$data['add_link'] = 'training';
$data['view_file'] = $this->training.'view_soal';
$this->load->view('template/media',$data);
}
when i want to return value about data_subjek_data to view model and can be displayed, what should i do?
$data_subjek_data will have the values of $data['data_subjek_data'] which is stored in your controller.
in the view you can use:
<?=$data_subjek_data->user_id ?>
OR
<?php
foreach($data_subjek_data as $row){
// loop with $row
}
?>
hello i need help please. mi code is error sorry for my bad englihs.
error : Fatal error: Call to a member function format() on boolean in C:\xampp\htdocs\aplicacion\application\views\guest\container.php on line 11
My container.php
My intention is to get the date of creation of the blog. Be added to the url of my website. And also the name of the post
<?php
foreach ($consulta->result() as $fila) {
?>
<div class="post-preview">
<?php
$date = DateTime::createFromFormat('YYYY-MM-DD', $fila->fecha);
$year = $date->format('Y',$date);
$name = str_replace('','_', $fila->post);
?>
<a href="<?php echo base_url()?>article/post/<?php echo $year ?>/<?php echo $name ?>">
article.php - controller
class article extends CI_Controller
{
public function post($year , $name)
{
// $fila = $this->post->getPostByName($id);
$fila = $this->post->getPostByYearAndName($year , $name);
if($fila == null){
echo "ERROR";
return;
}
$data = array('titulo' => $fila->post);
$this->load->view("guest/head" , $data);
$data = array('app' => 'blog');
$this->load->view("guest/nav" , $data);
$data = array(
'post' => $fila->post,
'descripcion' => $fila->descripcion,
'img' =>$fila->img);
$this->load->view("guest/header" , $data);
$data = array('contenido' => $fila->contenido);
$this->load->view("/guest/post" , $data);
$this->load->view("guest/footer");
}
post.php - MODEL
public function getPostByYearAndName($year = '', $name = '')
{
$result = $this->db->query("SELECT * FROM post WHERE year(fecha) = '$year' AND post LIKE '$name'");
return $result->row();
}
Your code works:
$date = DateTime::createFromFormat('y-m-d', '17-03-12');
echo $date->format('Y');
->
2017
if $date is false it means that your input $fila->fecha is not at the format YY-MM-DD.
If for example, your date is at the format YYYY-MM-DD you will have to use 'Y-m-d' instead.
Update:
Just to illustrate, if the input is a date but not at the specified format, for example:
$date = DateTime::createFromFormat('y-m-d', '2017-03-12');
echo $date->format('Y');
It reproduces OPs bug:
PHP Fatal error: Uncaught Error: Call to a member function format() on boolean in ~/tmp/date.php:5
So either $fila->fecha is empty, not a date or not at the format YY-MM-DD
I've been working on a method to email the users data to themselves and I'm having trouble printing data in the view. I can access all the data in bulk but I would like to format it so it displays within a HTML table for the user.
Model:
function getEmailData($usersid){
$this->db->select('*');
$this->db->from('symptom');
$this->db->where('userid', $usersid);
$symptomState = $this->db->get();
$result = $symptomState->result_array();
return $result;
}
function getEmailBMData($usersid){
$this->db->select('*');
$this->db->from('bowelmovement');
$this->db->where('userid', $usersid);
$bmState = $this->db->get();
$result = $bmState->result_array();
return $result;
}
Controller:
function sendDataAsEmail(){
$uid = $this->session->userdata('id');
$uname = $this->session->userdata('username');
$uemail = $this->input->post('email_data');
$data = array(
'userName'=> $uname,
);
$data['symptomData'] = $this->poomonitormodel->getEmailData($uid);
$data['bmData'] = $this->poomonitormodel->getEmailBMData($uid);
$data['symptomCount'] = $this->poomonitormodel->getTotalSymptomCount($uid);
$data['bmCount'] = $this->poomonitormodel->getTotalBMCount($uid);
var_dump($data);
$contents = $this->load->view('pooemail.php',$data,TRUE);
$this->email
->from('#', '#')
->to($uemail)
->subject('#')
->message($contents)
->set_newline("\r\n")
->set_mailtype('html');
$this->email->send();
}
View:
<p>
<?php foreach($symptomData as $data){
foreach($data as $nestdata){
echo $nestdata;
};
};?>
</p>
<p>
<?php foreach($bmData as $bmdata){
foreach($bmdata as $nestbmdata){
echo $nestbmdata;
};
};?>
</p>
Currently echo $nestdata outputs the data as a complete string like this, but I would like to access a single attribute like $nestdata['symptomdate']:
462016-04-1402:00burningOesophagus7vomitting 562016-04-1405:00tinglingGallBladder3vomitting 662016-04-1410:00shootingSmallIntenstine8vomitting 1362016-04-2016:47crampGallBladder1Gurgling Noise 1462016-04-2016:58tinglingRectum1Strange tingling sensation in the raer 1962016-04-2017:41crampIleum2Cramping 2062016-04-2017:42crampAnus7It whistles 2162016-04-2017:42crampRectum7It also whistles
But I would like to access a specific value so that I can put each bit of data into a table data cell so it's easier to read for the user.
Thanks!
The keys in $data will be the name of the var in the view. So, $data['symptomCount'] in the controller will be accessed as $symptomCount in the view.
What you do with it depends on its type. If $symptomCount is an object (class) then $x = $symptomCount->some_property. If it is an array - $symptomCount['some_index']
Iterate them like so
foreach($symptomData as $symptom){
echo $symptom['symptomdate'];
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am beginning to learn PHP OOP and I have this exercise to create a a function displaying a date.
I would like to come up with this result:
$date = new ("14-01-2014"); // for example, this date from user
echo $date -> displaDate ('YYYY-MM-JJ'); // Would result to 2014-01-14
echo $date -> day; //Would result to 14
echo $date -> dayOfWeek; //Would result to Thursday
Can someone please explain to me how to start making a function for this? I understand that functions has variables and methods.
<?php
$dt = new DateTime();
echo $dt->format('j-n-Y');
?>
If you're into implementing it yourself, you could go by doing it this way:
class Date
{
private $KeysArray;
// Members
public $day;
public $dayOfWeek;
public function __constructor()
{
// 1) This should get the current date and save it in the "keys array":
// the constants should of course be replaced with the method you're using to get
// the year, month or whatever.
$this->KeysArray = array("YYYY" => YEAR_VALUE, "MM" = MONTH_VALUE...)
// 2) Set all of the properties (data members):
$this->day = DAY_VALUE
$this->dayOfWeek = DAY_OF_WEEK_VALUE
}
public function DisplayDate($DateFormatString)
{
// 1) Parse the $DateFormatString. Turn it into an array of the terms. If it's
// always going to be split with "-", then you could use the function
// "explode()".
// 2) Go over that array and try calling each item with the $this->KeysArray.
}
}
From version 5.2, PHP has DateTime class for the representation of date and time.
Eg.
$dateTime=new DateTime('NOW');
echo "<pre>";
print_r($dateTime);
echo "</pre>";
http://www.php.net/manual/en/class.datetime.php
It's simple....
<!DOCTYPE html>
<html>
<body>
<?php
$t=time();
echo($t . "<br>");
echo(date('Y-m-d',$t));
?>
</body>
</html>
#Daniel Saad, thank you for helping me understand. Here is the solution to my question:
class Date{
private $dateString;
function __construct( $dateString )
{
$this->dateString = $dateString;
}
// Method to display date in different formats.
public function displayDate( $dateFormat )
{
list( $day, $month, $year ) = explode( '-', $this->dateString);
switch($dateFormat)
{
case "YYYY-mm-dd" : $format = $year.'-'.$month.'-'.$day; break;
case "mm-dd-YYYY" : $format = $month.'-'.$day.'-'.$year; break;
default : $format = $year.'-'.$month.'-'.$day;
}
return $format;
}
//Method to get day
public function day()
{
$day = date('d', strtotime($this->dateString));
return $day;
}
//Method to get the day of the week.
public function dayofweek()
{
$weekday = date('l', strtotime($this->dateString));
return $weekday;
}
//Method to get month in text.
public function getmonth()
{
$month = date('F', strtotime($this->dateString));
return $month;
}
}
?>
index.php
<?php
//Initialisation
$date = new Date('14-01-2014');
$anotherdate = new Date('13-07-1979')
?>
<h3>Instance 1</h3>
<p>Date: <?php echo $date->displayDate('YYYY-mm-dd'); ?></p>
<p>Day: <?php echo $date->day(); ?></p>
<p>Day of week: <?php echo $date->dayofweek(); ?></p>
<p>Month: <?php echo $date->getmonth(); ?></p> <br />
Result:
Instance 1
Date: 2014-01-14
Day: 14
Day of week: Tuesday
Month: January
I hope this could also help those who are just beginning to understand OOP.
So im running a query and trying to return a result set back into my view however when I do <?php foreach ($form_records as $form_record) : ?><?php echo $form_record['fname']; ?> <?php endforeach; ?> I get Undefined variable: form_records. What am i doing wrong when I run the query (Select fname, lname from form_manager where username = "tester") I get a row returned successfully so the query is working.
Model
function retrieve_form_record($username) {
$this->db->select('fname,lname');
$this->db->from('form_manager');
$this->db->where('username', $username);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$form_records= $query->result_array();
return $form_records;
}
else {
return false;
}
}
}
Controller
function load_saved_forms() {
$username = $this->tank_auth->get_username();
$form_records = $this->Form_model->retrieve_form_record($username);
$this->load->view('welcome',$form_records);
}
The problem is how you pass parameter to view, change this line:
$this->load->view('welcome',$form_records);
to :
$data['form_records'] = $form_records; //<<--pass data to view right way.
$this->load->view('welcome',$data);
and the then you can perform this inside view:
<?php foreach ($form_records as $form_record) : ?>
<?php echo $form_record['fname']; ?>
<?php endforeach; ?>
Note that $form_records in view is an index of array $datayou passed at $this->load->view('welcome',$data);
The $form_records is probably an array and looking at the codeigniter's doc it will turn those into array elements when it is passed to the view.
See this link: http://ellislab.com/codeigniter/user-guide/general/views.html
But it is saying if you did this in your controller:
$data['todo_list'] = array('Clean House', 'Call Mom', 'Run Errands');
$this->load->view('blogview', $data);
You can use that "todo_list" like this in your view:
<?php foreach ($todo_list as $item):?>
<li><?php echo $item;?></li>
<?php endforeach;?>