I am pulling the value EventDate from a table called events. At the moment is giving me a timestamp along with the date - 2012-05-29 00:00:00.
Is there anyway to trim this in PHP?
Try using MySQL's build in functions such as DATE_FORMAT() or even DATE()
SELECT
DATE_FORMAT(EventDate, '%Y-%m-%d') AS eventDate
FROM events;
SELECT
DATE(EventDate) AS eventDate
FROM events;
If don't want to store time stamp in DB,then change field type from datetime to date.Then while retrieving you will get only date.
OR
$date = //store DB date in this variable;
date('Y-m-d',strtotime($date));
If you really need to do this in PHP, use the date() function.
$date = '2012-05-29 00:00:00';
echo date('Y-m-d', strtotime($date)); // 2012-05-29
Related
I want to fetch old date as well not only current date stored in Timestamp,i used date() function but it only return current date and time.
I think it will work for you
$review_date=$data['review_date'];
$review_date=date('d M,Y', strtotime($review_date));
You can do this using strtotime and DateTime::createFromFormat(). I prefer the second one.
$timestamp = '2018-05-25T10:00:00';
$oDate = DateTime::createFromFormat('Y-m-dTH:i:s');
echo $oDate->format('Y-m-d');
I have dates formatted as d/m/y. How can I insert them into a DATETIME column?
Use MySQL's STR_TO_DATE() function:
INSERT INTO my_table VALUES (STR_TO_DATE('26/5/12', '%e/%c/%y'))
You need to use php's date() function along with strtotime() to convert date to any format you want.
MySQL database stores the date in YY-MM-DD format for datetime datatype, so if for example you have a date
$date = '26/05/2012';
You can convert it by using date() and strtotime()
$formatDate = date('Y-m-d', strtotime('26/05/2012'));
This will convert the date from 26/05/2012 to 2012-05-26 which then can be inserted into the database.
If you are using a timestamp datatype to store the date in your database, then all you need is to convert the current date into unix timestamp and store in database for example.
$date = strtotime('26/05/2012');
//this will convert the date to unix timestamp
Update:
as pointed out by #wallyk (thank you), strtotime() does not handles dd/mm/yy format. the fix is to replace the slash / by -m below code should work for you.
date('Y-m-d', strtotime(str_replace('/', '-', '26/05/2012')));
Try this:
$mysqldate = date("m/d/y g:i A", $datetime);
$date = date('d/m/Y');
$date = strtotime($date); //in unix time stamp format
Basically american date format is MM/DD/YYYY and you are providing DD/MM/YYYY so thats why startotime() returns you a null values on this input; and i prefer you must follow standard date format of american (MM/DD/YYYY) because if you are using mentioned format of date that will create more problems as well in different places ..
if you check by this
echo date('Y-m-d', strtotime('05/26/2012') );
and it is working fine ..
you could change your DATE column into a String Column and insert the data when ever you want 2 check if the date is right you can use a regular expression to do so
I have stored date time in mysql from a tutorial as:
2011-02-10 01:21:39
From this, how can I extract the individual elements using php?
Say I just want the year?
or even just the complete date if thats not possible?
See the strtotime() function. This will convert your date string into a timestamp. Then use date() to pull out the parts you need:
$time = strtotime('a string containing some description of the time and date');
$year = date('Y', $time);
Why not select the elements from MySQL directly?
select year(datefield), month(datefield), day(datefield) from yourtable
would return
+---------------+
| 2011 | 2 | 10 |
+---------------+
of course, if you want to do datemath in PHP, it'd be better to select the date as a UNIX_TIMESTAMP from mysql, which returns time in seconds from Jan 1/1970, which you can feed directly into PHP's date system;
SELECT UNIX_TIMESTAMP(datefield) ...
$timestamp = mysql_fetch(...)
$date = date($timestamp);
You can use the extract function of mysql from within your query like this:
SELECT EXTRACT(YEAR FROM yourDatefield) AS year,
EXTRACT(MONTH FROM yourDatefield) AS month,
EXTRACT(DAY FROM yourDatefield) AS day,
EXTRACT(HOUR FROM yourDatefield) AS hour,
EXTRACT(MINUTE FROM yourDatefield) AS minute,
EXTRACT(SECOND FROM yourDatefield) AS second
FROM
yourTable
Check out the extract function for more info.
You mean date('Y', strtotime('2011-02-10 01:21:39'));?
Go with #Marc's Solution if you are looping through MySQL result set
Use php's date(). You can format how ever you need.
date()
<?php $original = $row['time'];
$date = date_create($original);
echo date_format($date, 'Y');?>
just replace the $original with your own variable.
definitely check the php date() http://php.net/manual/en/function.date.php
Take a look at the Date and Time Functions for MySQL
For example, you can:
SELECT YEAR(DateColumn) FROM YourTable
date($format, strtotime($yourStringFromDB)) see PHP docs for format strings.
SELECT * FROM Dates
WHERE Date LIKE '%02%'
Using php I am inserting or updating the mysql database with create date or modified date using the variables
$datestring = "%Y:%m:%d %h:%i:%s";
$time = time();
$createdate= mdate($datestring, $time);
In this $createdate will be the variable I use to insert or update the table. But it's updating the wrong value. It's not the server time or localtime. Mostly it's 30 mins delay with the server's time.
Use date() function of PHP
$createdate= date('Y-m-d H:i:s');
Edit: after some googling it looks like you're using CodeIgniter. You should have mentioned that in your question.
The format string you're using doesn't match MySQL's date format. You want to use:
$datestring = '%Y-%m-%d %H:%i:%s';
use in mysql query like DATE_FORMAT(purchaseDate, "%Y:%m:%d %h:%i:%s") function
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')