beny lim
beny lim

Reputation: 1304

Substring in php through value retrieve from database

I am trying t0 perform a substring on DATETIME, the value of DATETIME is retrieve from database through mysql.

Example:

DATETIME: 2013-07-31 12:30:60 Year: 2013 Month: 07 Day: 31 hour: 12 minute: 30

My below code does not work. How should I go about doing it?

 <?php
$sql = "SELECT * FROM auctionItem;";
        // Write a statement to open a connection to MySQL server
        $link = mysql_connect("localhost:3306", "root", "gfg");
        // Write a statement to select the required database
        mysql_select_db("KXCLUSIVE", $link);
        // Write a statement to send the SQL statement to the MySQL server for execution and retrieve the resultset
        $resultset = mysql_query($sql);
        // Write a statement to close the connection
        mysql_close($link);

    $dateTime = $row["startTime"];
    $year = substr($dateTime, 0,4);
    $month = substr($dateTime, 5,7);
    $day = substr($dateTime, 8,10);
    $hour = substr($dateTime, 11,13);
    $minute = substr($dateTime, 14,16);
    echo "year " .$year."<br></br>";
    echo "month " .$month."<br></br>";
    echo "day " .$day."<br></br>";
    echo "hour " .$hour."<br></br>";
    echo "minute " .$minute."<br></br>";
    ?>

Upvotes: 1

Views: 1700

Answers (4)

HamZa
HamZa

Reputation: 14921

Using strtotime():

$time = '2013-07-31 12:30:60';
$timestamp = strtotime($time);

$year = date('Y', $timestamp);
$month = date('m', $timestamp);
$day = date('d', $timestamp);
$hour = date('H', $timestamp);
$minute = date('i', $timestamp);
$second = date('s', $timestamp);

echo $year.' '.$month.' '.$day.' '.$hour.' '.$minute.' '.$second.'<br>';

Using preg_split():

$time = '2013-07-31 12:30:60';
list($year, $month, $day, $hour, $minute, $second) = preg_split('/-|:|\s/', $time);
echo $year.' '.$month.' '.$day.' '.$hour.' '.$minute.' '.$second;

Upvotes: 0

hek2mgl
hek2mgl

Reputation: 158020

Use the DateTime class for this:

$dt = new DateTime('2013-07-31 12:30:60');
echo "year " .$dt->format('Y')."<br></br>";
echo "month " .$dt->format('m')."<br></br>";
echo "day " .$dt->format('d')."<br></br>";
echo "hour " .$dt->format('H')."<br></br>";
echo "minute " .$dt->format('i')."<br></br>";

Upvotes: 1

Hackerman
Hackerman

Reputation: 12295

Your code have a missing part:

<?php
   $sql = "SELECT * FROM auctionItem;";
        // Write a statement to open a connection to MySQL server
        $link = mysql_connect("localhost:3306", "root", "gfg");
        // Write a statement to select the required database
        mysql_select_db("KXCLUSIVE", $link);
        // Write a statement to send the SQL statement to the MySQL server for execution and retrieve the resultset
        $resultset = mysql_query($sql);
        // Write a statement to close the connection
        $row = mysql_fetch_assoc($resultset); //this was lost
        mysql_close($link);

    $dateTime = $row["startTime"];
    $year = substr($dateTime, 0,4);
    $month = substr($dateTime, 5,7);
    $day = substr($dateTime, 8,10);
    $hour = substr($dateTime, 11,13);
    $minute = substr($dateTime, 14,16);
    echo "year " .$year."<br></br>";
    echo "month " .$month."<br></br>";
    echo "day " .$day."<br></br>";
    echo "hour " .$hour."<br></br>";
    echo "minute " .$minute."<br></br>";
?>

PS: Dont use mysql extension....go for mysqli or PDO instead

Upvotes: 0

bwoebi
bwoebi

Reputation: 23777

substr wants as third parameter a length, not a position where to stop.

Correct would be:

$year = substr($dateTime, 0,4);
$month = substr($dateTime, 5,2);
$day = substr($dateTime, 8,2);
$hour = substr($dateTime, 11,2);
$minute = substr($dateTime, 14,2);

Upvotes: 2

Related Questions