$newTime = "TIME_FORMAT(date, '%e-%M-Y') AS date";
$result = mysqli_query($connect, "SELECT id, firstname, lastname, email, text, $newTime FROM gastenboek");
This is some information selecten from the database, i want to convert the sql timestamp to php days - months - years (22-11-2013).
The query works fine but date = NULL
it seems that my query is wrong, because when i do SELECT * FROM gastenboek it all works fine.
Error code: Notice: Trying to get property of non-object
I already googled the problem but i can't find the solution.
Sorry for my bad english, and thanks in advanced!
Greetings from Holland
You need to use DATE_FORMAT instead of TIME_FORMAT. And I think your format string needs to be tweaked. Try this:
DATE_FORMAT(date, '%e-%c-%Y') AS date
The docs for TIME_FORMAT say:
This is used like the DATE_FORMAT() function, but the format string may contain format specifiers only for hours, minutes, seconds, and microseconds. Other specifiers produce a NULL value or 0.
you have a , (comma) right before FROM (after $newtime)
Assuming the mysql timestamp will be equivalent as NOW() function output, you can use the following method:
SELECT concat(day(now()),"-",month(now()),"-",year(now())) as date
Not the most efficient approach but will get the job done.
Related
i have used Now() and it stores something like "2017-01-10 19:28:58" in database which is the current time of user's device.
But i want it like January 10 at 7:28pm . how to do it in simple way. please help
You can use PHP date function with its formatting options
<?php
echo date("F d \a\t g:ia");
If you want more detailed formatting please visit the PHP manual here. You can find everything you need with detailed examples.
you could use the PHP date function
date(F d \a\t\ g:ia);
Your post is ambiguous, but it sounds like you are referring to the SQL NOW() function. The format of the data stored could be a DATETIME or TIMESTAMP or a combination of DATE and TIME columns. The format you get when you retrieve this value from the database depends on that data storage format, your query, and how the value is dealt with when it gets into PHP.
If you want to reformat it using SQL, consider a function like DATE_FORMAT. Assuming your column with the date is called my_column, here's a sample query.
SELECT DATE_FORMAT(my_column, '%M %e at %l:%i%p');
EDIT: you can also use DATE_FORMAT on the result of the NOW() function:
SELECT DATE_FORMAT(NOW(), '%M %e at %l:%i%p');
You might have to tweak the second parameter to get the date format you want.
If your date is stored as a string (VARCHAR or whatever) in your database, then you would need to convert it to a timestamp or datetime first and then use the PHP date function to output the variant you want. Assuming $row is a record from your data table:
$date_string = $row["my_column"];
$stamp = strtotime($date_string); // NOTE that this will assume some timezone
if (!$stamp) {
die("Could not create a timestamp from the date");
}
echo date("F j \a\t g:ia);
You could also use PHP'S DateTime functions which are more modern, if somewhat verbose in usage.
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I know this question has been asked multiple times but nothing works for me.
I want to pull the date from the input and store it in my DB (MySQLi).
This is what I tried
$ses_s = $_POST['ses_date_s'];
$ses_n_s = date("Y-m-d", strtotime($ses_s));
$q = 'INSERT INTO date_t (ses_date_s,) VALUES (?)';
$stmt = mysqli_prepare($dbc, $q);
mysqli_stmt_bind_param($stmt, 's', $ses_n_s);
mysqli_stmt_execute($stmt);
But it won't work.
Date I enter: 17/07/2014 (echo $ses_s)
Date I get converted: 1970-01-01 (Unix start) ($ses_n_s)
What am I doing wrong?
P.S. How do I pull the date in revers, I want the date to be displayed d/m/Y and not Y/m/d (like in DB)?
Thanks in advance.
EDIT:
I found the problem up (thanks to Jonathan) i used dd/mm/yyyy not mm/dd/yyy
So if I can change my question how do I convert dd/mm/yyy to yyyy/mm/dd
Maverick you could explode the POST date which could then be put in the order you need the data:
$ses_s = $_POST['ses_date_s']; //'17/07/2014';
$sespieces = explode("/", $ses_s);
$ses_n_s = $sespieces[2]."-".$sespieces[1]."-".$sespieces[0]; //2014-07-17
You have two issues with the code.
1) issue with the insert statement, there's an errornous , after ses_date
2) date/time format passed in incorrectly.
$q = 'INSERT INTO date_t (ses_date_s,) VALUES (?)';
should be
$q = 'INSERT INTO date_t (ses_date_s) VALUES (?)';
The strtotime considers the locale, passing dd/mm/yyyy (canadian standard) isn't widely recognized, it's expecting it to be mm/dd/yyyy (us standard) in most cases.
I would recommend that you don't rely on strtotime to create timestamps, and would pass in the data in the mysql preferred format, or pass in an actual timestamp itself.
As to actually get the date from the database, strtotime will work fine on data from mysql as it's going to be recognized correctly.
So when you pull from the db you can do this.
echo date('d/m/Y' strtotime($date));
$_POST['ses_date_s'] is a string but date() requires an integer (Unix timestamp). You can convert the former to the latter using strtotime() although it only expects dates to follow the American convention of mm/dd/yy. The DateTime object has more sophisticated facilities for parsing dates.
I have a column of type date (only date) in mysql. However, when I am using the following:
$Answer->dateCreated=date('d-m-y');
I'm getting an error
A non well formed numeric value encountered
Any idea??
MySQL's date format is yyyy-mm-dd, which in PHP would be date('Y-m-d'). Your format string is reversed and using 2 digit years instead of 4 - Y2k's old news by now... don't use 2 digit years anymore.
I just did this and it is working:
$Answer->dateCreated = strtotime("now");
Try:
define('MYSQL_DATE_FORMAT', 'Y-m-d H:i:s');
$Answer->dateCreated = date(MYSQL_DATE_FORMAT);
I understand you're not wishing to save the time, don't worry MySQL will ignore the time for these columns.
Try using $Answer->dateCreated=date('Y-m-d');
i have column named postDate defined as timestamp.
when i print it directly:
echo $result['postDate'];
i do get that what is stored(eg. 2011-03-16 16:48:24)
on the other hand when i print it through date function:
echo date('F/j/Y',$result['postDate'])
i get December/31/1969
what am i doing wrong?
many thanks
try this.
date('F/j/Y',strtotime($result['postDate']));
as timestamp is required, not formatted date as second parameter.
or you can also try
SELECT UNIX_TIMESTAMP(postDate) as postDateInt from myTable
instead of SELECT postDate from myTable
and then have this in your code.
date('F/j/Y',$result['postDateInt']);
The PHP date function looks for an int time() as the 2nd param. Try using strtotime()
echo date('F/j/Y', strtotime($result['postDate']) );
Why not format the date as needed in your MySQL query?
SELECT DATE_FORMAT(postDate, '%M/%D/%Y') as date from table
The PHP `date()' function expects a number for the second parameter - ie a unix timestamp.
You can convert a SQL date string (or virtually any other date string) into a timestamp in PHP by using the strtotime() function. At least two other answers have already suggested this.
However, I would suggest that you'd be better off getting the date out of your database in unix timestamp format in the first place. You can do this by querying using the MySQL UNIX_TIMESTAMP() function, as follows:
SELECT UNIX_TIMESTAMP(mydatefield) AS mydatefield_timestamp FROM mytable
..obviously, replacing the field and table names as appropriate.
Then you will get the date in timestamp format in your returned dataset in PHP, which you can pass directly into the date() function as follows:
echo date('F/j/Y',$result['mydatefield_timestamp']);
Hope that helps.
I want to allow my users to search the database for a row that was submitted on a certain day. The date is entered into the field in the database using the date() function which is great, but when i use the php strtotime function, of course the dates are not exactly the same.
Is there some clever mysql function that can help me with this?
I had an issue with this before.
You're best to generate a start and end date then use the BETWEEN function instead.
So something like this:
$start_date = "2009-01-01 00:00:00";
$end_date = "2009-01-01 23:59:59";
$sql = "SELECT * FROM table WHERE date BETWEEN '$start_date' AND '$end_date' AND id = 'x';
Of course you would just pull your dates from the DB using strtotime and append the time stamps - depends how you used date()
Hope this helps :)
You can use MySQL's DATE() function to extract date part of the timestamp:
select ... from table ... where DATE(date_column) = '2010-01-25';
If you have problem submitting '2010-01-25' from PHP, you can use PHP's date function with 'Y-m-d' as parameter to only get the date part.
$d = date('Y-m-d', strtotime(...));
Looking at your question closely, it seems you'll need both of those. First use PHP's date function to get only the date part and then use MySQL's date to match only those records.
PHP:
$timestamp_from_php = strtotime('December 25, 2009');
SQL:
select
`fields`
from
Table t
where
date_format('Y-m-d', t.`datetime_field`) = date_format('Y-m-d', '$timestamp_from_php')