I want to update my databse to change all the different formats of dates into one format. I want to update different time formats into the format 2016-12-22.
I get this error:
250 Fatal error: Uncaught exception 'Exception' with message
'DateTime::__construct(): Failed to parse time string ($res1) at
position 0 ($): Unexpected character' in
/hermes/walnaweb13a/b775/moo.manhassurinder/singhaniafarm/test.php:22
Stack trace: #0
/hermes/walnaweb13a/b775/moo.manhassurinder/singhaniafarm/test.php(22):
DateTime->__construct('$res1') #1 {main} thrown in
/hermes/walnaweb13a/b775/moo.manhassurinder/singhaniafarm/test.php on
line 22
$squery = "SELECT date,id FROM `addCutting` ";
$sresult = mysqli_query($con,$squery);
while($row = mysqli_fetch_assoc($sresult))
{
"<br/>". $res1= $row['date'];
echo"<br/>". $res= $row['id'];
/* if($res1!= date('y/m/d'))
{
$result2= date_format( new DateTime($res1), 'y/m/d' );
echo $result2;
}
}
*/
/* $date1 = new DateTime($res1);
echo $date1->format('Y-m-d'); echo "<br/>"; */
$date = new DateTime('$res1');
echo $date->format('Y-m-d');
}
while($row = mysqli_fetch_assoc($sresult))
{
$date= $row['date'];
$formated_date = date('Y-m-d',strtotime($date));
echo $formated_date;
}
Try this hope it works
Referal Link on strtotime click here
I think what you what is this instead of the last 2 lines
$formated = "";
if(!emtpy($res1)){
$date = new DateTime($res1);
if(!emtpy($date)){
$formated = $date->format('Y-m-d');
}
}
echo $formated;
Related
<?php
function find_days($start_date, $end_date) {
$response = new stdClass();
try {
$sdate = new DateTime($start_date);
$edate = new DateTime($end_date);
$dateInterval = $edate->diff($sdate);
$response->status = true;
$response->result = $dateInterval;
return $response;
} catch (Exception $e) {
$response->status = false;
$response->result = 'Invalid Date Format';
return $response;
}
}
?>
Start Date: <input type="date" name="sdate" placeholder="start date" />
End Date: <input type="date" name="edate" placeholder="end date" />
<input type="submit" value="Find Days" />
<?php
if (isset($_POST['sdate']) && $_POST['sdate']) {
$start_date = $_POST['sdate'];
$end_date = $_POST['edate'];
//now call the function
$days_array = find_days($start_date, $end_date);
if ($days_array->status) {
echo " <input type='text' name='day'
value='.$days_array>result>days.' />";
$day = $_POST['day'];
$query = "INSERT into cart (date,edate,days)
VALUES('$start_date','$end_date','$day')";
$success = $conn->query($query);
if (!$success) {
die("Couldn't enter data: ".$conn->error);
}
} else {
echo $days_array->result;
}
}
My code is working perfectly. But the result is displayed only on the screen.
So I've tried to store the result by placing it in a textbox and then insert into the table in a usual way. But I got an error "Catchable fatal error: Object of class DateInterval could not be converted to string in C:\xampp\htdocs\date.php on line 45"
I don't know how to rectify this.. please help me solve this.
You have to convert it to a string using format:
<?php
$now = new DateTime();
$hour = new DateTime( "now +1hours +13minutes +22seconds" );
$diff = $now->diff( $hour );
echo $now->format('Y-m-d H:i:s');
echo "\n";
echo $hour->format('Y-m-d H:i:s');
echo "\n";
echo $diff->format('%H:%I:%S');
Output
2018-07-26 20:53:42
2018-07-26 22:07:04
01:13:22
I'm getting the error "SQLSTATE[HY093]: Invalid parameter number" when I try to run this query:
<?php
include "../config/c_config.php";
$db = dbConn::getConnection();
$pmuid = $_POST['txtuid'];
$pnml = $_POST['txtnm'];
$ptgll = $_POST['txttgl'];
$jk = $_POST['optjk'];
$palamat = $_POST['txtalamat'];
$idv = $_POST['cbprov'];
$idc = $_POST['cbcity'];
$pkode = $_POST['txtkodepos'];
$pphone= $_POST['txtnophone'];
$pemkof= $_POST['txtmailinf'];
$pimg = $_POST['fileimg'];
$pdate=date("Y-m-d H:i:s");
// Update data on mysql
$sqlep = "UPDATE str_user_mamber_profile SET
profile_nama_lengkap=:pnml,
profile_jk=:jk,
profile_tgl_lahir=:ptgll,
profile_alamat=:palamat,
id_provinsi=:idv,
id_city=:idc,
profile_kodepos=:pkode,
profile_phone=:pphone,
profile_email_konfirmasi=:pemkof,
profile_date=:pdate
WHERE mamber_unique_id = :pmuid";
$qep = $db->prepare($sqlep);
$qep->execute(array(":pnml"=>$pnml,
":jk"=>$jk,
":ptgll"=>$ptgll,
":palamat"=>$palamat,
":idv"=>$idv,
":idc"=>$idc,
":pkode"=>$pkode,
":pphone"=>$pphone,
":pemkof"=>$pemkof,
":pdate"=>$pdate,
":pmuid"=>$pmuid));
if($qep){
echo 'work';
}else{
echo 'not work';
}
?>
after i run,this query give a result "WORK" but not update the db. i try search same question about this error but not help me to fixed. can you help me see what is wrong with the query?
Thank you
change
":pnml"=>$pnml,
to
"pnml"=>$pnml,
everywhere in "execute" method
$qep->execute(array("pnml"=>$pnml,
"jk"=>$jk,
"ptgll"=>$ptgll,
"palamat"=>$palamat,
"idv"=>$idv,
"idc"=>$idc,
"pkode"=>$pkode,
"pphone"=>$pphone,
"pemkof"=>$pemkof,
"pdate"=>$pdate,
"pmuid"=>$pmuid));
$qep is a statement object, and will always be truthy. You need to capture the result of executing the statement:
$result = $qep->execute(...);
if ($result){
echo 'work';
} else {
echo 'not work';
var_dump($qep->errorInfo());
}
Using $_POST values without checking that they are set may give you lots of warnings, as well.
To get timestamps to be in the user's time period, I have set up a timezone conversion tool with PHP for my posting system. Basically the user sets their own timezone through a dropdown, and this is stored on the database as a PHP timezone - so if I select GMT, the value in the database would be Europe/London.
The idea is then to take this from the database and plug it into a timezone conversion piece of code, but this presently isn't converting anything at all. Can anyone see the problem?
doMessage.php
$date = date("Y-m-d G:i:s");
$user_tz = $result['time'];
$schedule_date = new DateTime($date, new DateTimeZone($user_tz) );
$schedule_date->setTimeZone(new DateTimeZone('UTC'));
$date = $schedule_date->format('Y-m-d H:i:s');
core/init.php
<?php
session_start();
$db = new PDO('this is all correct');
if(isset($_SESSION['id'])) {
$i = $_SESSION['id'];
$query = $db->prepare("SELECT * FROM users WHERE id=:i");
$query->bindParam(":i",$i);
$query->execute();
$result = $query->fetch();
$ver = $result["activated"];
if($ver == 0) {
echo "Please <a href='activate.php'>verify your account.</a>";
}
}
else {
echo "Please <a href='login.php'>log in.</a>";
$guest = True;
}
To get current date and time of GMT, just this is enough:
date_default_timezone_set('GMT');
$current_date = date("Y-m-d h:i:s A T")
echo $current_date;
This outputs 2013-12-30 5:30:20 AM GMT
It's whining of line 6: Warning: PDO::__construct() expects parameter 2 to be string, array given
Along with a line 7 error:
Fatal error: Call to a member function prepare() on a non-object in
How do I fix this? I've tested the query and it works fine..
<?php
## Loop through results from mysql
try{
#connection string
$dbconn = new PDO('mysql:host=localhost;port=3306;dbname=thedb',array(PDO::ATTR_PERSISTENT => true));
$q = $dbconn->prepare("SELECT thecol FROM thetbl");
#call stored proc
$q->execute();
#get the rows into an array
$result = $q->fetchAll();
foreach($result as $r){
$xmlUrl = $r['FW_ArtSrcLink'];
$ConvertToXml = simplexml_load_file($xmlUrl);
# -> Setup XML
$newsStory = $ConvertToXml->channel->item;
}
# -----> Load News Stories
for($i = 0;$i<sizeof($newsStory); $i++){
# Source of Article Info-->
$SrcTitle=$newsStory[$i]->title;
$SrcLink=$newsStory[$i]->link;
# Actual News Article Info -->
$title=$newsStory[$i]->title;
$desc=$newsStory[$i]->description;
# Output Results ------------>
echo '<hr>';
echo '<strong>'.'Title:'.$title.'</strong>'.'(via: <a href=\''.$SrcLink.'\'>'.$SrcTitle.'</a>'.'<br />';
//echo 'Link:'.$link.'<br />';
echo 'Description'.$desc.'<br>';
echo '<hr>';
}
} // try
catch(Exception $e){
$errorStored = $e->getMessage() . ' on ' .'/errors/fanwire_loop.php'; #where errors are stored
$pageDateOfError = '/aggregate_looping.php'.date('l jS \of F Y h:i:s A'); #inc the file and date into the file too
file_put_contents($errorStored,$pageDateOfError, FILE_APPEND | LOCK_EX);
} // catch
#Load in File
/*************************************************
$xmlUrl ="http://sports.espn.go.com/espn/rss/mlb/news";
$ConvertToXml = simplexml_load_file($xmlUrl);
# -> Setup XML
$newsStory = $ConvertToXml->channel->item;
# -----> Load News Stories
for($i = 0;$i<sizeof($newsStory); $i++){
// Source of Article Info-->
$SrcTitle=$newsStory[$i]->title;
$SrcLink=$newsStory[$i]->link;
// Actual News Article Info -->
$title=$newsStory[$i]->title;
$desc=$newsStory[$i]->description;
echo '<hr>';
echo '<strong>'.'Title:'.$title.'</strong>'.'(via: <a href=\''.$SrcLink.'\'>'.$SrcTitle.'</a>'.'<br />';
//echo 'Link:'.$link.'<br />';
echo 'Description'.$desc.'<br>';
echo '<hr>';
}
***********************************************/
?>
You're initializing the PDO object incorrectly, the second parameter of the constructor should be the username, not an array of options.
$dbconn = new PDO('mysql:host=localhost;port=3306;dbname=thedb',array(PDO::ATTR_PERSISTENT => true));
should be,
$dbconn = new PDO('mysql:host=localhost;port=3306;dbname=thedb',
'yourusername',
'yourpassword',
array(PDO::ATTR_PERSISTENT => true));
See the PHP manual page for PDO::__construct() for more information.
The second error you're getting because the $dbconn object wasn't created properly due to the first error.
Can u try
$dbconn = new PDO('mysql:host=localhost;port=3306;dbname=thedb');
$q = $dbconn->prepare("SELECT thecol FROM thetbl", array(PDO::ATTR_PERSISTENT => true));
i am experiencing some weird behaviour here.
i am using the following code to reference the facebook api.
$query = "SELECT msg, user_id, comment_time FROM comments WHERE aid = '$aid' ORDER BY comment_time DESC";
$result = mysql_query($query) or die("ERROR: $query.".mysql_error());
if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_object($result)){
$uidval = $row->user_id;
$posterInfo = $facebook->api_client->users_getInfo($uidval, array('name', 'pic_square_with_logo', 'profile_url'));
$nameuser = $posterInfo[0]['name']; //this is line 50
$pic = $posterInfo[0]['pic_square_with_logo'];
$profile_url = $posterInfo[0]['profile_url'];
echo '<img src="'.$pic.'" />';
echo ''.$nameuser.'';
echo '<br>';
echo $row->comment_time;
echo '<br>';
echo $row->msg;
}
}
it gives me this error:
Fatal error: Cannot use string offset as an array in /home/amitver/public_html/roadies/comments.php on line 50
but surprisingly i am using the exact same code successfully at the top of my page. why this weird behaviour. this is the code at the top of page:
//connect to fB
$uid = $user_id;
$userInfo = $facebook->api_client->users_getInfo($user_id, array('name', 'pic_square'));
$nameuser = $userInfo[0]['name'];
$pic = $userInfo[0]['pic_square'];
I think that sometimes theusers_getInfo is returning an array, while other times it is returning a string. Probably it only returns a simple string if only one result is available.
Try this:
$nameuser = ($posterInfo[0]) ? $posterInfo[0]['name'] : $posterInfo['name'];
this will happen if $posterInfo is actually an empty string ('').
can you var_dump($posterInfo) in the loop and check what it's doing...