I have a string of a date:
25/08/2012
And here I'm trying to convert it to a DateTime object in order to save it to the MySQL database. My backend schema has a DateOfPrint date column ready to receive this data.
Here's what I've tried:
$eventDate = DateTime::createFromFormat('d/m/y', $fecha->innertext);
echo $eventDate;
The echo statement doesn't show anything on the screen, and when trying to save it to the database, nothing is saved in that column.
Any suggestions?
Your $eventDate contains a boolean(false) which is printed as empty string.
You need to use an upper-case Y.
Y A full numeric representation of a year, 4 digits Examples: 1999 or 2003
y A two digit representation of a year Examples: 99 or 03
And you have to call DateTime::format();
e.g.
<?php
$fecha = new StdClass;
$fecha->innertext = '25/08/2012';
$eventDate = DateTime::createFromFormat('d/m/Y', $fecha->innertext);
if ( false===$eventDate ) {
die('invalid date format');
}
echo $eventDate->format('Y-m-d');
prints
2012-08-25
You need to format it for a MySQL column before you can insert it:
// Need upper case Y here, thanks to VolkerK for pointing that out
$eventDate = DateTime::createFromFormat('d/m/Y', $fecha->innertext);
$eventDate = $eventDate->format( 'Y-m-d'); // I think this is the correct format
Then you can use $eventDate to save the date to the database.
$eventDate is an object, not a string. You will need to access the properties of the element in your code to be able to correctly insert it's value into a table or echo it out. On that note, you could use a var_dump($eventDate); which should show you all there is to know about the object.
You can reference the PHP docsm on the DateTime class to get the available properties and see which one best fits your needs.
$eventDate = DateTime::createFromFormat('d/m/Y', $fecha->innertext);
echo $eventDate->format('Y-m-d');
short answer
$st_time = date('Y-m-d H:i',$yourdate);
if you want only day month and year use this
$st_time = date('Y-m-d',$yourdate);
Related
Ive checked the leads here and have not found the right solution,
<?php
$mysqldate = $row['callbackdate'];
$phpcurrentdate = strtotime( $mysqldate );
if ( $phpcurrentdate == date("Y-m-d") ) {
echo $row['last_name'];
}else{
echo "Nothing";
}
?>
The date field in sql is date and the format is YYYY-mm-dd. The answer always being returned is "Nothing". I know this sort of question has many variations but Ive had no luck finding this type. Im not looking for a range sort just a simple match is all... and the data $row['callbackdate']is functioning as Ive tested all other connections.
So Id appreciate any help! Thanks,
Les
$mysqldate = "2017-08-28"; //example
$phpcurrentdate = DateTime::createFromFormat('Y-m-d', $mysqldate); //put your format
$today= new DateTime("now");
if ( $phpcurrentdate == $today ) {
echo 'last_name';
}else{
echo "Nothing";
}
The dates are stored as string in your database. As possible solution to this is formatting both as dateTime for example:
$today = date('Y-m-d H:i:s');
$databaseDate = '2017-08-06 20:50:38';
var_dump(new DateTime($today) > new DateTime($databaseDate)) // returns true
var_dump(new DateTime($today) < new DateTime($databaseDate)) // returns false
Another way to do this is to make your columns type in the database a DateTime. This way you only have to format the current date as a DateTime and then you can compare them
i'm very new to this Forum. I'm working on my own website and got a problem.
Because i'm very new to coding and not very skilled with php i can't find a solution for this little problem.
I would like to formate my date from the Database to a "friendlydate"
e.g. Date from Database: 2016-06-08 00:00:00
my wish-date: 08.06.2016
Here is my Code from the viewmanager, where i want do define the
"friendlydate"
// assign values to view object
$viewBlog->id = $value->id;
$viewBlog->bild = $value->bild;
$viewBlog->date = $value->date;
$viewBlog->author = $value->author;
$viewBlog->title = $value->title;
$viewBlog->text = $value->text;
$viewBlog->category_id = $value->category_id;
if (strlen($value->text) > 280) {$viewBlog->shorttext = substr($value->text,0,280)."...";} else {$viewBlog->shorttext = $value->text;}
***$viewBlog->friendlydate = here is my problem;***
$viewBlog->objCategory = $this->getViewCategory($value->category_id);
You can parse your original date in to a DateTime object which will then allow you to format the date however you like. For instance:
$date = new DateTime($value->wish-date);
$viewBlog->friendlydate = $date->format('Y-m-d H:i:s');
In this case, friendlydate would be 2016-06-08 00:00:00. To see how to specify what format you like see the documentation.
Assuming $viewBlog->friendlydate is your date variable,
$viewBlog->friendlydate = date("m.d.Y");
where m is numeric representation of a month, with leading zeros, n is numeric representation of a month without leading zeros and Y is a full numeric representation of a year output as 4 digits.
Using string functions:
$parts = explode('-', substr('2016-06-08 00:00:00', 0, 10));
$date = $parts[2].'.'.$parts[1].'.'.$parts[0];
This will convert the string as you have described. You may also want to look into PHP date functions.
You will just need to reformat your date. I am really fond of the DateTime method in php.
// Get the current date with its format
$date = DateTime::createFromFormat('Y-m-d H:i:s', $value->date);
// Convert it to a new format
$viewBlog->date = $date->format('d.m.Y');
In the resource below you can find information about different formats in which you can output your date.
Resources
DateTime - Manual
I want to store a specific date in a variable. If stored like $x="01/01/2016" it is acting as a string from which I cannot extract a part, like from getdate() year, month, day of the month, etc.
Use the DateTime object:
$dateTime = new DateTime('2016/01/01');
To get only parts of the date you can use the format method:
echo $dateTime->format('Y'); // it will display 2016
If you need to create it from the format you wrote in the question, then you can use the factory method createFromFormat:
$dateTime = DateTime::createFromFormat('d/m/Y', '01/01/2016');
echo $dateTime->format('Y/m/d');
This is work for me
$date = '20/May/2015:14:00:01';
$dateInfo = date_parse_from_format('d/M/Y:H:i:s', $date);
$unixTimestamp = mktime(
$dateInfo['hour'], $dateInfo['minute'], $dateInfo['second'],
$dateInfo['month'], $dateInfo['day'], $dateInfo['year'],
$dateInfo['is_dst']
);
this is what you are looking for http://php.net/manual/en/class.datetime.php
You can use $myDate = new DateTime('01/01/2016'); to declare date. To get year, month and date from the specified date, use echo $myDate->format('d m Y');
Change the format based on your need. To know more about date format refer
I have to create an array with date/time of each row.
I am using the following code. "TIME_STAMP" is the field which contains MySQL Timestamp (CURRENT_TIMESTAMP default).
while ( $row = mysql_fetch_array($result) )
{
$timestamp = strtotime($row["TIME_STAMP"]);
$x[] = sprintf("%s, %s", date ('j/n', $timestamp), date('H:i', $timestamp));
}
If i make a SELECT * from mysql client all is ok: TIME STAMP is correct:
2013-06-04 17:11:43
but when I pass this array to JpGraph (to make horizontal axis) all the records are displayed with
1/1 01:00
The code for the graph:
$graph = new Graph("1024", "800");
$graph->SetScale( 'textlin');
$graph->img-> SetMargin(60,40,40,110);
$graph->xaxis->SetTickLabels( $x );
$graph->xgrid->Show(true);
Any hint?
My guess here is that the value you're dealing with is a string, rather than an actual DateTime object. You might want to create a new DateTime object with that string before passing it along to the next step.
http://www.php.net/manual/en/function.date-create.php
You have:
$graph->SetScale( 'textlin');
it should be:
$graph->SetScale( 'datlin');
Fixed
My select query didn't include "TIME_STAMP" field; the graph was added later, but query wasn't updated with new attributes for JpGraph.
Thanks anyone for the help.
So I have a field in my database called 'DateTime' and the following lines of code:
echo "Date/Time: ";
echo $row['DateTime'];
How do I format it so that instead of being like this:'2013-02-07 22:14:56', it will be like this: '07/02/13 - 22:14'
Thanks.
Alternatively you could use:
DateTime::createFromFormat('Y/m/d H:i:s',$row['DateTime']); this will give you a datetime object, which are quite nice to work with.
Another alternative would be to have MySQL format the DATETIME value as a string in the desired format, using the DATE_FORMAT function.
SELECT DATE_FORMAT(`DateTime`,'%d/%m/%y - %H:%i') AS `DateTime`
...
No change required to your PHP code except for the SQL text sent to the database server.
This approach can very efficient, and reduce the amount of code you need, if all you are doing with this string is displaying it. If you are doing any sort of manipulation on this value, then casting the string value returned from MySQL resultset into a datetime object is probably a better way to go.
A demonstration of the DATE_FORMAT function:
SELECT DATE_FORMAT('2013-02-07 22:14:56','%d/%m/%y - %H:%i') AS `DateTime`
DateTime
----------------
07/02/13 - 22:14
how to output date into Year textbox Month textbox Day textbox
$book_date = $myrow["Publication_Day"];
$book_year = Date("Y", strtotime($book_date));
$timestamp contains ur date & time in any format.....................
date('Y/m/d - H:i',strtotime($timeStamp));
echo date('d/m/y H:i', strtotime($row['DateTime']));
See date and strtotime for more detail on the functions from the docs
$mytime = strtotime('2013-06-07 22:14:56');
$newDate = date('m/d/y - G:i', $mytime);
echo $newDate;
Here's an alternative using DateTime. If you're working with timezones this code can be easily modified to handle that.
$datetime = new DateTime('2013-02-07 22:14:56');
echo $datetime->format('d/m/y H:i');
See it in action