Re: Formatting Date (m/d/yyyy) To Timestamp

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I tried and minimize your question to the following code:

<?php

$date_from_sheet = "1/1/2021";
// As you know the format of the date, it is easier to convert based on that
$timestamp = DateTime::createFromFormat('d./m/Y', $date_from_sheet);

// Instead of inserting the values inside the string, use bind variables instead.
$query = "INSERT INTO some_table(date_field) values (?)";
// I'm assuming $mysqli is already instantiated elsewhere
$stmt = $mysqli->prepare($query);
// The only field in the query is the date field,
// so it is clear that the only parameter is the corresponding value
$stmt->bind_param($timestamp);
// The only remaining thing to do is execute it.
$stmt->execute();
?>

Does this enable you to solve your issue?

I used the following sources:
https://www.php.net/manual/en/mysqli-stmt.bind-param.php
https://www.php.net/manual/en/datetime.createfromformat.php

Kind regards/met vriendelijke groet,

Serge Fonville

http://www.sergefonville.nl


Op ma 11 jan. 2021 om 02:18 schreef Talin Sharma <talinsharma.in@xxxxxxxxx>:
I have also tried: $d1 = date("Y-m-d H:i:s", strtotime($d)); But that returns random dates when I insert into the database... What is wrong?


Thanks:
     Talin Sharma


On Sun, Jan 10, 2021 at 7:10 PM Talin Sharma <talinsharma.in@xxxxxxxxx> wrote:
Also here is the excel sheet I am using for testing.
Thanks:
     Talin Sharma


On Sun, Jan 10, 2021 at 7:05 PM Talin Sharma <talinsharma.in@xxxxxxxxx> wrote:
I am attaching all of my code. How do I enter the timestamp to the database then?

Thanks:
     Talin Sharma


On Sun, Jan 10, 2021 at 7:01 PM Serge Fonville <serge.fonville@xxxxxxxxx> wrote:
It doesn't seem to work. What I did: $d1 = 'strtotime('1/1/2021'); echo $d1;' Echos: '980053200' That in human form is: 'November 30, -0001' Not what I want... any idea why?
You are converting the timestamp to a string again using echo, this outputs a unix epoch. You should instead treat $d1 as

Perhaps if you share a bigger part of your code, it would be easier to be of help.

 

Kind regards/met vriendelijke groet,

Serge Fonville

http://www.sergefonville.nl


Op ma 11 jan. 2021 om 00:51 schreef Talin Sharma <talinsharma.in@xxxxxxxxx>:
It doesn't seem to work. What I did: $d1 = 'strtotime('1/1/2021'); echo $d1;' Echos: '980053200' That in human form is: 'November 30, -0001' Not what I want... any idea why?


Thanks:
     Talin Sharma


On Sun, Jan 10, 2021 at 6:30 PM Serge Fonville <serge.fonville@xxxxxxxxx> wrote:
Hi Talin,

Would https://www.php.net/manual/en/function.strtotime.php solve your conversion problem?

Kind regards/met vriendelijke groet,

Serge Fonville

http://www.sergefonville.nl


Op ma 11 jan. 2021 om 00:18 schreef Talin Sharma <talinsharma.in@xxxxxxxxx>:
Hi All,

I have a string: '1/1/2021'. I want to turn this into a timestamp for entering into MySql. I have tried everything I could find. Please let me know how to proceed. 

Also, let me know if you need more information.


Thanks:
     Talin Sharma

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux