I am pulling a date value from a MySQL DB formatted as 01/20/13 I am calling the PHP date function on this value returned to get what day of the week it is, so 01/20/13 is today's date which is Sunday but it keeps returning the value Wednesday. I have included the code below I am new to programming so this is probably a stupid error I am overlooking.
<?php
require '../TimeCard/DB.php';
try{
$stmt = $conn->prepare('SELECT `date` FROM `timeRecords` WHERE `employeeID`= 1 ');
$stmt->execute();
} catch(PDOException $e){
echo'ERROR: ' . $e->getMessage();
}
while($row = $stmt->fetch())
{
echo date("l", $row['date']) . "<br>";
echo $row['date'] . "<br>";
}
?>
Mysql does not store dates as m/d/y it stores them as Y-m-d you're mysql database will turn "01/20/13" into 0000-00-00.
However, if you are not using the date type, and storing as a string use
strtotime($row['date'])
use strtotime on your mysql stored date, then use the date function on it
$day = date('l',strtotime($row['date']));
try with strtotime()
echo date("l", strtotime($row['date'])) . "<br>";
The second argument to PHP's date() function is an integer timestamp. You'd have better luck using DateTime, eg
$dt = DateTime::createFromFormat('m/d/y', $row['date']);
echo $dt->format('l');
This gives you the added bonus of tailoring the date parser to match your source format rather than relying on strtotime() which definitely has its quirks such as treating dates with forward-slashes (10/12/13) as US (12th October) vs dates with hyphens (10-12-13) as EU (10th December)
Example here - http://codepad.viper-7.com/iVXxA1
Related
I have an Oracle query which returns date string in the format Y-m-d H:i:s and I need to pass them to the Series::AddXY method. How can I do that?
The "CandleChart.php" example in the Features demo shipped with the product uses DateTimes on the horizontal axis.
Here a variation:
<?php
//Includes
include "../../../../sources/TChart.php";
$chart1 = new TChart(600,450);
$chart1->getChart()->getHeader()->setText("Candle Style");
$chart1->getChart()->getAspect()->setView3D(false);
// Clip Series points
$chart1->getChart()->getAspect()->setClipPoints(true);
$chart1->getChart()->getLegend()->setVisible(false);
// Add Candle data using doubles for date values
$today = time();
$day = 86400;
$hour = 3600;
$chart1->getAxes()->getBottom()->setIncrement(DateTimeStep::$ONEMINUTE);
$chart1->getAxes()->getBottom()->getLabels()->setDateTimeFormat('d/m/Y H:i:s');
$chart1->getAxes()->getBottom()->getLabels()->setAngle(90);
$candle=new Candle($chart1->getChart());
$chart1->setAutoRepaint(false);
for ($i=$today;$i<($today+$hour);$i+=60) {
$candle->addCandle($i,rand(0,100),rand(0,100),rand(0,100),rand(0,100));
}
$chart1->setAutoRepaint(true);
$chart1->doInvalidate();
$chart1->render("chart1.png");
$rand=rand();
print '<font face="Verdana" size="2">Candle Chart Style<p>';
print '<img src="chart1.png?rand='.$rand.'">';
?>
The problem is that I do not have constant time intervals and I can not use a "time machine" as in the Candle example .
The time (X value) I have comes from an Oracle query:
$query = "SELECT ptm.IDENTIFICACAO,
mtr.SERIAL,
TO_CHAR(rtu.DATAHORA, 'yyyy-mm-dd hh24:mi:ss') AS DATAHORA,
So the DateTime value is a string in the PHP date format : Y-m-d H:i:s, which I need to convert to TChart values. I do not know if I am full correct but it
seems that DateTime values should be entered as float values (Unix Timestamp)
So I am converting them as follows:
while( ($row = oci_fetch_array($stmt, OCI_ASSOC)) != false ){
$thetime = DateTime::createFromFormat('Y-m-d H:i:s', $row["DATAHORA"]);
if($thetime)
$tchart->getChart()->getSeries(0)->addXY((float) $thetime->getTimestamp() , $row["ENERTOT"] / 1000);
}
++$rowCount;
}
I hope this can help someone else.
Best regards.
I have the code:
$month = DateTime::createFromFormat('m/Y', $date);
if ($month) {
$month = $month -> format('01/m/Y');
}
echo "From ".$startMonth." to ".$endMonth = date("t/m/Y", strtotime($month));
when $date is a string like "05/2015".
This returns:
From 01/05/2015 to 31/01/2015
But for some reason the month is coming up as 01 when it should be 05?
Why is it doing this? It should be
From 01/05/2015 to 31/05/2015
Besides the unnecessary chopping and changing between DateTime objects and unix timestamps (when you could do the whole thing using DateTime objects.... you're passing a formatted date of '01/m/Y' to strtotime()... the / indicates to the strtotime() function that the date is in US date format (mm/dd/yy): and you should use '01-m-Y' (with a -) for dd-mm-YYYY if you want to tell strtotime() that it's a European format date.
See the "localized notations" table on the PHP Docs page for an explanation of formats accepted by strtotime()
However, doing the whole thing using DateTime objects:
$date = '4/2015';
$month = DateTime::createFromFormat('m/Y', $date);
echo "From " . $month->format('01/m/Y') . " to ". $month->format('t/m/Y');
I have a MySQL database with a list of dates. I want to output all these dates, provided they occur after today, into a page. The dates are stored in the database in DATE format, as Y-m-d.
I've got the following code (excluding the query etc):
$dateToday = date('Y-m-d');
do{
$dateCompare = new DateTime($row['date']);
if ($dateCompare > $dateToday){
echo '<p>'.$dateCompare -> format('Y-m-d').'</p>';
} else {
echo '<p>FALSE</p>';
}
}while ($row = $stmt->fetch(PDO::FETCH_ASSOC));
But this just outputs all the dates, including one I have set in the past for testing purposes. What am I doing wrong?
$dateToday is a string. $dateCompare is a DateTime object.
You should use strtotime() function.
http://www.w3schools.com/php/func_date_strtotime.asp
Try and convert the date from myssql to a datetime object and output.
$changetime = new DateTime($time, new DateTimeZone('UTC'));
ECHO $changetime->format('m/d/y h:i a');
// list of timezones http://us1.php.net/manual/en/timezones.php
I actually use this to output all my MYSQL datetime data - allows me to convert to any timezone. Note, this will assume your datetime is in UTC - you should convert to your timezone.
$stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach ($stmt as $row) {
$time = strtotime($row['date']);
if ($_SERVER['REQUEST_TIME'] - $time) {
echo '<p>'. date('Y-m-d', $time) . '</p>';
} else {
echo '<p>FALSE</p>';
}
}
In PHP, how do you convert a mysql timestamp like this "1125443836"
to an xml/date-time like this:
<wp:post_date>2011-01-25 02:10:32</wp:post_date>
UPDATE:
The columns in the mySQL are stored as int(10).
Based on samples below, this is what I tried with two sample values in my database.
Something is wrong, maybe hash or Unix dates stored in mySQL table?
$testSqlDateStamp = "1125443836";
echo "<BR>DateTest=".date('Y-m-d G:i:s',strtotime($testSqlDateStamp));
$testSqlDateStamp = "1125444107";
echo "<BR>DateTest=".date('Y-m-d G:i:s',strtotime($testSqlDateStamp));
echo "<BR>";
Results:
DateTest=1969-12-31 18:00:00
DateTest=1969-12-31 18:00:00
Second Update: worked without calling strtotime
$testSqlDateStamp = "1125444107";
echo "<BR>DateTest=".date('Y-m-d G:i:s',$testSqlDateStamp);
$testSqlDateStamp = "1125443836";
echo "<BR>DateTest=".date('Y-m-d G:i:s',$testSqlDateStamp);
Results:
DateTest=2005-08-30 18:21:47
DateTest=2005-08-30 18:17:16
'Y-m-d G:i:s' is not it. At least not in 2015 (I realize this is old).
In php 5.2.0 the DateTime class has a constant for this:
echo 'XML time: ' . date( DateTime::RFC3339, time() ) . '<br>';
DateTime::RFC3339 is set to "Y-m-d\TH:i:sP".
date("Y-d-m G-i-s",$time);
That should do it.
http://php.net/manual/en/function.date.php
EDIT: this won't work for mySQL timestamps, they must be converted to unix timestamps via strtotime() info: PHP: strtotime - Manual.
<?php echo '<wp:post_date>' . date('Y-m-d G:i:s',strtotime($yourMysqlDateStampVar)) . '</wp:post_date>';
I have a date/time string like this: 180510_112440 in this format ddmmyy_hhmmss
I need a snippet for having a string formatted like this way: 2010-05-18 11:24:40
Thanks for help.
another possible answer is the common use of strptime to parse your date and the mktime function:
<?php
$orig_date = "180510_112440";
// Parse our date in order to retrieve in an array date's day, month, etc.
$parsed_date = strptime($orig_date, "%d%m%y_%H%M%S");
// Make a unix timestamp of this parsed date:
$nice_date = mktime($parsed_date['tm_hour'],
$parsed_date['tm_min'],
$parsed_date['tm_sec'],
$parsed_date['tm_mon'] + 1,
$parsed_date['tm_mday'],
$parsed_date['tm_year'] + 1900);
// Verify the conversion:
echo $orig_date . "\n";
echo date('d/m/y H:i:s', $nice_date);
$inDate = '180510_112440';
$date = strtotime('20'.substr($inDate,4,2).'-'.
substr($inDate,2,2).'-'.
substr($inDate,0,2).' '.
substr($inDate,7,2).':'.
substr($inDate,9,2).':'.
substr($inDate,11,2));
echo date('d-M-Y H:i:s',$date);
Assumes date will always be in exactly the same format, and always 21st century
list($d,$m,$y,$h,$i,$s)=sscanf("180510_112440","%2c%2c%2c_%2c%2c%2c");
echo "20$y-$m-$d $h:$i:$s";