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
Related
Can anyone help me? this is my controller, when i try to store 'purchase' data it show this message strtotime() expects parameter 1 to be string, array given
The error was on the $purchase->date = date('Y-m-d', strtotime($request->date)); and $purchaseDetail->date = date('Y-m-d', strtotime($request->date));
public function store(Request $request){
if($request->category_id == null){
return redirect()->back()->with('error', 'Please Purchase The Product');
} else{
// Multipale Data Insert start //
$purchase = new purchase();
$purchase->purchase_no = $request->purchase_no;
$purchase->date = date('Y-m-d', strtotime($request->date));
$purchase->description = $request->description;
$purchase->status = '0';
$purchase->created_by = Auth::user()->id;
DB::transaction(function() use($request,$purchase) {
if($purchase->save()) {
// Purchase Details Insert Start //
$category_id = count($request->category_id);
for ($i=0; $i < $category_id; $i++) {
$purchaseDetail = new purchaseDetail();
$purchaseDetail->date = date('Y-m-d', strtotime($request->date));
$purchaseDetail->purchase_id = $purchase->id;
$purchaseDetail->supplier_id = $request->supplier_id[$i];
$purchaseDetail->category_id = $request->category_id[$i];
$purchaseDetail->product_id = $request->product_id[$i];
$purchaseDetail->buying_qty = $request->buying_qty[$i];
$purchaseDetail->unit_price = $request->unit_price[$i];
$purchaseDetail->buying_price = $request->buying_price[$i];
$purchaseDetail->discount_amount = $request->discount_amount[$i];
$purchaseDetail->ppn = $request->ppn[$i];
$purchaseDetail->status = '0';
$purchaseDetail->save();
}
}
});
}
// Redirect
return redirect()->route('purchase.view')->with('success', 'Purchase Added Successfully');
}
Hello, i try to change $purchase->date = date('Y-m-d', strtotime($request->date)); into $purchase->date = $request->date;
But the result was TypeError
Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in D:\Project Laravel\alc-pos\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 869
As for my opnion you must not format your date when saving to database.
Instead you can format it when outputit from database into your blade templates.
Anyway make a check on $request->date, seems to be an array.
You can format your date using Laravel accessor in your model like this (example):
public function getFormattedStartDateAttribute()
{
return $this->start_date->format('d-m-Y');
}
and then call it in your blade template like this (example):
<input type="text" name="start_date" class="form-control" value="{{$trip->formatted_start_date}}">
More info about Laravel Mutators & Accessors : Laravel Docs
Also you can do another thing. On your schema::create make:
$table->date('date_name_column');
and then just save it like follow:
$purchaseDetail->date = $request->input('date');
This way the date in your database will be save as follow : YYYY-MM-DD
Hope it helps!
This question already has answers here:
PHP & Case Sensitivity [duplicate]
(3 answers)
Closed 4 years ago.
I am trying to use Class with Public Function and my query is coming back with following error:
"Notice: Undefined variable: articles in C:\xampp\htdocs\CMS\index.php on line 21"
I have done a print_r to show the result from class and everything looks correct.
"Array ( [0] => Array ( [article_id] => 1 [article_title] => Title [article_content] => Content [article_timestamp] => 2019-02-02 13:33:03 ) )"
On my class function, I have tried placing the query into variable then return that variable out of class, but error message.
If I bypass the class function, and place the query result into $Articles then it works.
line 1-6 /Index.php
<?php
include_once('includes/article.php');
$Article = new Article;
$Articles = $Article->fetch_all();
print_r($Articles)
?>
line 17-21 /Index.php
<?php foreach ($articles as $article) { ?>
<ol>
<li><?php echo $article['article_title']; ?> - <small><?php echo date('l F jS, Y', strtotime($article['article_timestamp'])); ?></small></li>
</ol>
<?php } ?>
line 5-8 /includes.php
class Article {
public function fetch_all() {
return DB_query("SELECT * FROM articles");
}
}
line 51-70 /includes.php
function DB_query($query, $params = []) {
$conn = DB_connect();
if ($params)
{
$stmt = $conn->prepare($query);
$types = str_repeat('s', count($params));
$stmt->bind_param($types, ...$params);
$stmt->execute();
$result = $stmt->get_result();
} else {
$result = mysqli_query($conn, $query);
}
if ($result)
{
$result = mysqli_fetch_all($result,MYSQLI_ASSOC);
return $result;
} else {
return mysqli_affected_rows($conn);
}
}
The Result should be echo $article['article_id'] will return ID, echo $article['article_title'] will return title and finally echo date('l F jS, Y', strtotime($article['article_timestamp'])) will return date.
From the manual:
Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.
You need to use either $Articles or $articles in both places.
I made CRUD laravel by including the date on the form, but I got an error in the controller, and the contents of the controller:
public function update(Request $request, $id)
{
$suratmasuk= \App\SuratMasuk::find($id);
$suratmasuk->no_surat=$request->get('no_surat');
$suratmasuk->no_agenda=$request->get('no_agenda');
$tgl_surat=date_create($request->get('tgl_surat'));``
$format = date_format($tgl_surat,"Y-m-d");
$suratmasuk->tgl_surat = strtotime($format);
$tgl_terima=date_create($request->get('tgl_terima'));
$format = date_format($tgl_terima,"Y-m-d");
$suratmasuk->tgl_terima = strtotime($format);
$suratmasuk->sumber_surat=$request->get('sumber_surat');
$suratmasuk->perihal=$request->get('perihal');
$suratmasuk->keterangan=$request->get('keterangan');
$suratmasuk->save();
return redirect('surat-masuk');
}
$stamp= time();
$daystotal = date("Y-m-d h:i:s",$stamp);
$postdate = date_create(date_create_from_format('Y-m-d',$daystotal))
echo date_format($daystotal,"j M,y H:i");
Output : "j M, y H:i
Help me, i wanna put the timestone to this format Hour:minutes:secondes
$services = $pdo ->prepare("SELECT * FROM `servicos` WHERE `DE` = :u");
$services->bindValue(":u", "1");
$services->execute();
echo $numServico = $services->rowCount(); // Contagem de serviços
$l = $services->fetchAll(PDO::FETCH_OBJ);
//$json = json_encode($l);
echo '{"data":[';
//echo $json;
foreach ($l as $listar) {
$inicioHora = new DateTime("listar->REGISTRO");
$inicioHora = $inicioHora->format('H:i:s');
}
But i receive this
Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (listar->REGISTRO) at position 0 (l): The timezone could not be found in the database' on line 26
if the field $listar->REGISTRO is a timestamp, then you only need to use the date() function http://php.net/manual/en/function.date.php
$time = date('H:i:s', $listar->REGISTRO);
Replace this:
$inicioHora = new DateTime("listar->REGISTRO");
$inicioHora = $inicioHora->format('H:i:s');
With this:
$inicioHora = date('H:i:s', strtotime($listar->REGISTRO));
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