how could i get date from varchar column in mysql - php

I want to get date from VARCHAR column.
(eg: 4/14/2018 12:00:00 AM)
How do I display only date
(eg: 4/14/2018)?
SELECT date(created_at) from self_balance
here created_at(varchar)
this returns NULL value

You can run this query to get your output,
SELECT DATE_FORMAT(STR_TO_DATE(created_at, "%Y-%m-%d"), "%Y-%m-%d") FROM
self_balance
First I am matching date format and converting it to date and then formatting.

You can fetch date like a normal string from the database then you need to use strtotime which parses an English textual DateTime into a Unix timestamp. Then you can use
date function which returns the formatted date string. I have passed a static string. You can pass your string variable which you are fetching from the database
$time = strtotime($date_string_from_database);
<?php
$time = strtotime('4/14/2018 12:00:00 AM');
$newformat = date('m/d/Y',$time);
echo $newformat;
?>
You can see the live demo here

Related

Convert excel decimal number into date

I have a number on which after applying some formula I got 43917.60417. Excel convert this number into date and datetime format as 3/27/2020 14:30:00. How can I convert this number into date or datetime using mysql or php. I searched a lot but didn't got any solution.
I tried
SELECT FROM_UNIXTIME(REPLACE('13577.77916', '.', ''));
-- this results in 2013-01-10 06:01:56
SELECT FROM_UNIXTIME(REPLACE('43917.60417', '.', ''));
-- this results in null
Here you go in PHP
<?php
$unix = (43917.60417 - 25569) * 86400; //convert to unix first
echo gmdate("d-m-Y H:i:s", $unix); //return to your desired date format
?>
AND IN MySQL, This should work for both positive and negative epoch
SELECT DATE_ADD(FROM_UNIXTIME(0), interval (13577.77916-25569)*86400 second);
And to specify date format in MySQL, you can use DATE_FORMAT(
SELECT DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), interval (13577.77916-25569)*86400 second),'%Y-%m-%d') as my_new_date;
The DateTime extension dt knows a special method createFromMsTimestamp for the MS Excel timestamp.
$excelTimestamp = 43917.60417;
$date = dt::createFromMsTimestamp($excelTimestamp);
echo $date->format('Y-m-d H:i:s');
//2020-03-27 14:30:00

Unable to save the exact timestamp from form

I'm trying to save events into my table that contain date_start and date_end timestamps. From another PHP page, I get four inputs: two date inputs and two time inputs. I'm trying to insert these values but I keep getting the following value on inserting it:
0000-00-00 00:00:00
Here's my script:
$rep=$bdd->query('SELECT * FROM association WHERE nom="'.$_SESSION['usertag'].'"');
$data=$rep->fetch();
$debut="\'".$_POST['date_start']." ".$_POST['time_start'].":00\'";
$fin="\'".$_POST['date_end']." ".$_POST['time_end'].":00\'";
$debut=date('Y-m-d H:i:s');
$fin=date('Y-m-d H:i:s');
$timestamp_debut =strtotime($debut);
$timestamp_fin = strtotime($fin);
$req=$bdd->query('INSERT into evenement values (NULL,'.$_POST['name'].','.$_POST['content'].',\''.$timestamp_debut.'\',\''.$timestamp_fin.'\','.$data['id'].')');
PHP's strtotime() outputs a Unix timestamp. If your database column type is DATETIME, a Unix timestamp is not the correct format. You can just insert the formatted date() string.
For example:
strtotime(date('Y-m-d H:i:s'))
1562117846
date('Y-m-d H:i:s')
2019-07-02 21:36:40
For MySQL:
MySQL recognizes DATETIME and TIMESTAMP values ... [a]s a string in either 'YYYY-MM-DD hh:mm:ss' or 'YY-MM-DD hh:mm:ss' format. A "relaxed" syntax is permitted here, too: Any punctuation character may be used as the delimiter between date parts or time parts. For example, '2012-12-31 11:30:45', '2012^12^31 11+30+45', '2012/12/31 11*30*45', and '2012#12#31 11^30^45' are equivalent.
String and Numeric Literals in Date and Time Context
However, it seems that you're using the current timestamp when you might want to use the values posted from your form. PHP's date() uses "the current time if no timestamp is given".
If you want to use your posted values instead, you can indeed use strtotime() to convert them to Unix timestamps and then date() to format them.
$date = '2019-07-02';
$time = '15:28';
date('Y-m-d H:i:s',strtotime($date.' '.$time));
2019-07-02 15:28:00
Alternatively, I might recommend using PHP's DateTime class:
$date = '2019-07-02';
$time = '15:28';
$datetime = new Datetime($date.' '.$time);
echo $datetime->format('Y-m-d H:i:s');
2019-07-02 15:28:00

How to convert date into integer number in php?

I have an mysql database with a date column and i am using datatype datetime.
But now i want change my datatype from datetime to long integer
I would like to know how to convert date to any integer value.
Let say i have a date
i.e 2012-03-27 18:47:00
so I was wondering if it's possible to convert into any integer number like 131221154
Use strtotime function of PHP.
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.
echo strtotime('2012-03-27 18:47:00'); //--> which results to 1332866820
And to make it back again, just use the date function of PHP:
$long = strtotime('2012-03-27 18:47:00'); //--> which results to 1332866820
echo date('Y-m-d H:i:s', $long);
check this
<?php
$timestamp = strtotime('1st January 2004'); //1072915200
// this prints the year in a two digit format
// however, as this would start with a "0", it
// only prints "4"
echo idate('y', $timestamp);
?>

Datetime format PHP and MySQL

I have the following problem. I have a string, which contains time and date as follows:
dd/MM/yy hh:mm:ss
This I have in PHP. And then I have a MySQL database, which has a column of a datetime format, where I need to insert this value. Obviously the problem is, that the format is different, so instead of the actual date, it results in a field "0000-00-00 00:00:00".
Could you please help me with converting this string and then inserting it properly into MySQL?
For the MySQL I use the standard INSERT INTO command.
From the DATETIME documentation:
MySQL retrieves and displays DATETIME values in YYYY-MM-DD HH:MM:SS format.
I'd use PHP's DateTime class and DateTime::createFromFormat() method, and convert the data into a MySQL-compatible date string, like so:
$date = DateTime::createFromFormat('d/m/Y H:i:s', $yourDateString);
$dateToBeInserted = $date->format('Y-m-d H:i:s');
Write a function to convert date,
function sqldate($date)
{
$sql_date = date('Y-m-d H:i:s',strtotime($date));
return $sql_date;
}
And your query look like,
$query = "INSERT INTO tableName (dateColumn) VALUES('".sqldate($date)."') ";
You can convert it using STR_TO_DATE() along with your INSERT statement, eg.
INSERT INTO tableName (dateColumn)
VALUES(STR_TO_DATE('dd/MM/yy hh:mm:ss','%d/%m/%y %H:%i:%s'))
SQLFiddle Demo
Try this:
$parsedDate = date('Y-m-d H:i:s', strtotime($yourDate));
// Insert $parsedDate into your table column Date

If I have a PHP string in the format YYYY-DD-MM and a timestamp in MySQL, is there a good way to convert between them?

I'm interested in doing comparisons between the date string and the MySQL timestamp. However, I'm not seeing an easy conversion. Am I overlooking something obvious?
Converting from timestamp to format:
date('Y-m-d', $timestamp);
Converting from formatted to timestamp:
mktime(0, 0, 0, $month, $day, $year, $is_dst);
See date and mktime for further documentation.
When it comes to storing it's up to you whether to use the MySQL DATE format for stroing as a formatted date; as an integer for storing as a UNIX timestamp; or you can use MySQL's TIMESTAMP format which converts a numeric timestamp into a readable format. Check the MySQL Doc for TIMESTAMP info.
You can avoid having to use strtotime() or getdate() in PHP by using MySQL's UNIX_TIMESTAMP() function.
SELECT UNIX_TIMESTAMP(timestamp) FROM sometable
The resulting data will be a standard integer Unix timestamp, so you can do a direct comparison to time().
I wrote this little function to simplify the process:
/**
* Convert MySQL datetime to PHP time
*/
function convert_datetime($datetime) {
//example: 2008-02-07 12:19:32
$values = split(" ", $datetime);
$dates = split("-", $values[0]);
$times = split(":", $values[1]);
$newdate = mktime($times[0], $times[1], $times[2], $dates[1], $dates[2], $dates[0]);
return $newdate;
}
I hope this helps
strtotime() and getdate() are two functions that can be used to get dates from strings and timestamps. There isn't a standard library function that converts between MySQL and PHP timestamps though.
Use the PHP Date function. You may have to convert the mysql timestamp to a Unix timestamp in your query using the UNIX_TIMESTAMP function in mysql.
A date string of the form:
YYYY-MM-DD
has no time associated with it. A MySQL Timestamp is of the form:
YYYY-MM-DD HH:mm:ss
to compare the two, you'll either have to add a time to the date string, like midnight for example
$datetime = '2008-08-21'.' 00:00:00';
and then use a function to compare the epoc time between them
if (strtotime($datetime) > strtotime($timestamp)) {
echo 'Datetime later';
} else {
echo 'Timestamp equal or greater';
}

Categories